Каковы различия между .on
,. listenTo
и .bind
?
Я тестировал их здесь, и они, похоже, делают одно и то же: обратный вызов.
var NewStatusView = Backbone.View.extend({
events: {
"submit form": "addStatus"
},
initialize: function(options) {
// using .on
//this.collection.on("add", this.clearInput, this);
// or using bind:
//_.bindAll(this, 'addStatus', 'clearInput');
//this.collection.bind('add', this.clearInput);
// or using listenTo:
_.bindAll(this, 'addStatus', 'clearInput');
this.listenTo(this.collection, 'add', this.clearInput) ;
},
addStatus: function(e) {
e.preventDefault();
this.collection.create({ text: this.$('textarea').val() });
},
clearInput: function() {
this.$('textarea').val('');
}
});
Когда и в сценарии использовать, что лучше?