"this" 在 backbone 的 listenTo() 中指的是什么
What refer "this" to in backbone's listenTo()
我写了一些代码...
var View = Backbone.View.extend({
...
initialize: function () {
this.listenTo(collection, 'add', this.addOne)
},
addOne : function (item) {
this // 'this' here refers... what?
})
我读了 guide
在本指南中,listenTo 上的 "this" 指的是 "listener",但我测试了上面的代码,"this" 可能是视图本身
在"events and views"部分,
"If the event is bound using listenTo() then within the callback this refers to the listener."
以下片段来自文档
http://backbonejs.org/#Events-listenTo
listenToobject.listenTo(other, event, callback)
告诉对象监听另一个对象上的特定事件。使用这种形式而不是 other.on(event, callback, object) 的优点是 listenTo 允许对象跟踪事件,并且它们可以在以后一次性全部删除。将始终以对象作为上下文调用回调。
view.listenTo(model, 'change', view.render);
很好地解释了 this
是指对象 listenTo
被调用。
例如:object.listenTo(...)
将对象作为上下文,即 this
将是回调中的对象。
上述问题中的代码片段本质上是在视图对象上调用 listenTo,因此 this
实际上是视图
我写了一些代码...
var View = Backbone.View.extend({
...
initialize: function () {
this.listenTo(collection, 'add', this.addOne)
},
addOne : function (item) {
this // 'this' here refers... what?
})
我读了 guide
在本指南中,listenTo 上的 "this" 指的是 "listener",但我测试了上面的代码,"this" 可能是视图本身
在"events and views"部分,
"If the event is bound using listenTo() then within the callback this refers to the listener."
以下片段来自文档 http://backbonejs.org/#Events-listenTo
listenToobject.listenTo(other, event, callback)
告诉对象监听另一个对象上的特定事件。使用这种形式而不是 other.on(event, callback, object) 的优点是 listenTo 允许对象跟踪事件,并且它们可以在以后一次性全部删除。将始终以对象作为上下文调用回调。
view.listenTo(model, 'change', view.render);
很好地解释了 this
是指对象 listenTo
被调用。
例如:object.listenTo(...)
将对象作为上下文,即 this
将是回调中的对象。
上述问题中的代码片段本质上是在视图对象上调用 listenTo,因此 this
实际上是视图