Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

javascript - In Backbone how to get click event on an anchor tag within a list item

In a backbone view within the render function I have an anchor tag with a list item inside a unordered list. in short

var DemoView = Backbone.View.extend({
  el: '#body',
  
  render: function() {
    this.$('sidebar-nav > ul').append($('<li class="divider"></li>'));
    this.$('.sidebar-nav > ul')
      .append($('<li><a href="' + _.path(Overture.contextPath, '/calendars/', 
    this.controller.model.id,'/preview') + '" class="internal-link">Link 1</a></li>'));
    this.$('.sidebar-nav > ul')
        .append($('<li><a href="#" class="internal-link">Link 2</a></li>'));
  } 
});

I want to open an alert box on the click on Link 2 without navigating to some other page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can setup the View's events:

var DemoView = Backbone.View.extend({
  el: '#body',
  events: {
    'click .alert-link': onAlertLinkClick
  },
  onAlertLinkClick: function() {
     // put your alert code here
  },
  render: function() {
    this.$('sidebar-nav > ul').append($('<li class="divider"></li>'));
    this.$('.sidebar-nav > ul')
      .append($('<li><a href="' + _.path(Overture.contextPath, '/calendars/', 
    this.controller.model.id,'/preview') + '" class="internal-link">Link 1</a></li>'));
    this.$('.sidebar-nav > ul')
        .append($('<li><a href="#" class="internal-link alert-link">Link 2</a></li>'));
  } 
});

You have to add the alert-link to the second anchor to select it of course.

See docs for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...