最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How does the new Backbone View 'events' hash work with function values instead of strings in versio

matteradmin4PV0评论

The Backbone 0.9.0 changelog says:

A view's events hash may now also contain direct function values as well as the string names of existing view methods.

When I try the following it fails, saying that the value for the event is undefined.

var BB = Backbone.View.extend({
  'initialize': function() {

    this.$el.html('<input type="button" value="Click me!" />');
    jQuery('body').html(this.el);
  },

  'events': {
    'click input[type="button"]': this.buttonClicked
  },

  'buttonClicked': function() {
    alert('button clicked!');
  }

});

window.b = new BB()

Am I misunderstanding the new feature? Can somebody explain how it works differently than I expected? Perhaps it's just my JavaScript syntax/value of 'this' at definition time that is borked.

The way I'm used to doing it still works:

'events': {
  'click input[type="button"]': 'buttonClicked'
},

The Backbone 0.9.0 changelog says:

A view's events hash may now also contain direct function values as well as the string names of existing view methods.

When I try the following it fails, saying that the value for the event is undefined.

var BB = Backbone.View.extend({
  'initialize': function() {

    this.$el.html('<input type="button" value="Click me!" />');
    jQuery('body').html(this.el);
  },

  'events': {
    'click input[type="button"]': this.buttonClicked
  },

  'buttonClicked': function() {
    alert('button clicked!');
  }

});

window.b = new BB()

Am I misunderstanding the new feature? Can somebody explain how it works differently than I expected? Perhaps it's just my JavaScript syntax/value of 'this' at definition time that is borked.

The way I'm used to doing it still works:

'events': {
  'click input[type="button"]': 'buttonClicked'
},
Share Improve this question edited Feb 25, 2014 at 1:56 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Feb 2, 2012 at 18:39 aw crudaw crud 8,89121 gold badges76 silver badges117 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

When the JavaScript parser gets here:

'events': {
  'click input[type="button"]': this.buttonClicked
},

this is probably window, not an instance of BB as you expect. The window object doesn't have a buttonClicked property (at least it doesn't in your case) so you're really saying this:

'events': {
  'click input[type="button"]': undefined
},

and there's your error.

If you look at the source for delegateEvents, you'll see what the ChangeLog means:

delegateEvents: function(events) {
  // ...
  for (var key in events) {
    var method = events[key];
    if (!_.isFunction(method)) method = this[events[key]];
    // ...
  }
},

That _.isFunction call is what you're interested in. That means that you can say things like this:

events: {
  'click input[type="button"]': function() { alert('pancakes!') },
  'click button': some_function_that_is_in_scope
}

So you can put defined functions (either by their name if they're accessible or as function literals) in the events lookup table.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far