我正在努力学习 Backbone 0.9.9 中的新变化。
目前我遇到了解释 listenTo
和 on
的不同之处的问题:
listenTo
var View = Backbone.View.extend({
tagName: "div",
intialize: function() {
this.listenTo(this.model, 'change', this.render);
},
render: function() {
this.$el.empty();
this.$el.append('<p>hello world</p>');
}
});
在
var View = Backbone.View.extend({
tagName: "div",
intialize: function() {
this.model.on('change', this.render, this);
},
render: function() {
this.$el.empty();
this.$el.append('<p>hello world</p>');
}
});
我听说listenTo
与stopListening
一起使用可以在例如视图被移除以避免内存泄漏时取消订阅所有事件。
这是唯一的原因吗?