Backbone 0.9.9:listenTo 和 on 之间的区别 [英] Backbone 0.9.9: Difference between listenTo and on
问题描述
我正在尝试学习他们在 Backbone 0.9.9 中所做的新更改.
I am trying to learn the new changes they did in Backbone 0.9.9.
目前我在理解 listenTo
和 on
之间的区别时遇到了问题:
Currently I got problems to understand the difference between listenTo
and on
:
聆听
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
取消订阅所有事件,例如当视图被删除以避免内存泄漏时.
I have heard that listenTo
allows with stopListening
to unsubscribe from all events when for example the view gets removed to avoid memory leaks.
这是唯一的原因吗?
推荐答案
listenTo
和 stopListening
基本上来自社区.它们有助于更轻松地绑定和解除绑定事件.
listenTo
and stopListening
came from the community, basically. They help to make it easier to bind and unbind events.
围绕这个想法有很多现有的文档和博客文章,包括我写的关于这个主题的东西.
There's a lot of existing documentation and blog posts surrounding the idea, including stuff that I've written on the subject.
Johnny Oshika 是我看到的第一个使用这种技术的人.它最初是作为 StackOverflow 问题的答案发布的:Backbone.js:重新填充或重新创建视图?
Johnny Oshika is the first person that I saw using this technique. It was originally posted as an answer to a StackOverflow question here: Backbone.js : repopulate or recreate the view?
你可以在这里阅读我写的关于这个的内容:
You can read what I've written about this, here:
这篇关于Backbone 0.9.9:listenTo 和 on 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!