Backbone 0.9.9:ListenTo和on之间的区别 [英] Backbone 0.9.9: Difference between ListenTo and on
问题描述
目前我有问题要了解ListenTo和on之间的区别:
listenTo
var View = Backbone.View .extend({
tagName:div,
intialize:function(){
this.listenTo(this.model,'change'渲染);
},
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取消订阅所有事件,例如视图被删除避免内存泄漏。
这是唯一的原因吗?
问候,
bodo
listenTo
和 stopListening
来自社区,基本上。它们有助于使绑定和解除绑定事件更容易。
有很多现有的文档和博客文章围绕着这个想法,包括我在这个主题上写的内容
Johnny Oshika是我看到使用这种技术的第一个人。它最初是作为StackOverflow问题的答案发布在这里:骨干.js:重新填充或重新创建视图?
您可以在这里阅读我写过的内容:
I am trying to learn the new changes they did in Backbone 0.9.9.
Currently I got problems to understand the difference between ListenTo and 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>');
}
});
on
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>');
}
});
I have heard that listenTo allows with stopListening to unsubscribe from all events when for example the view gets removed to avoid memory leaks.
Is this the only reason?
Regards, bodo
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 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.EventBinder: Better Event Management For Your Backbone Apps
- Zombies! RUN! (Managing Page Transitions In Backbone Apps)
- Backbone.js And JavaScript Garbage Collection
这篇关于Backbone 0.9.9:ListenTo和on之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!