0
我正在使用RequireJS和Backbone,並使用listenTo
收聽collection
的add
事件。我無法弄清楚如何將this
作爲我所處視圖的實例,在這種情況下爲GroupView
。引用事件回調中包含的骨幹視圖?
define([
'underscore',
'backbone',
'handlebars',
...
...
], function(_,Backbone,Handlebars,...){
...
...
var GroupsView = Backbone.View.extend({
el: "#current-item",
collection: Groups,
model: Group,
groupTemplate: Handlebars.compile(GroupTemplate),
events: {
"click #add-group" : "addGroupClicked",
},
initialize: function(){
this.listenTo(Groups,'add',this.addGroup,this);
},
addGroup: function(group){
//I want a reference to this instance of GroupsView here.
//if I use this... it references
//Uncaught TypeError: Object [object global] has no method 'groupTemplate'
console.log(this.groupTemplate);
//Uncaught TypeError: Object [object global] has no method 'redrawGroups'
--> console.log(this.redrawGroups);
},
redrawGroups: function(){
},
...
...
'bindAll'解決方案確實對我有用。然而,第一個只有'bind'的例子對我來說似乎不起作用。我不明白爲什麼它不起作用。謝謝你,先生! – Johnston
奇怪的是,它們應該是相當的......很高興你能夠正常工作。 – machineghost
另外我不知道你可以做'_(this).bindAll('addGroup')'。我認爲它必須是'_.bindAll(this,'addGroup')'。 – Johnston