var ListView = Backbone.View.extend({
el: $('hello'),
initialize: function() {
var stuff = new FieldCollection();
var output;
stuff.parse();
stuff.fetch({
success: function (collection, response) {
console.log(response);
output=response;
return response;
}
});
this.render(output);
},
render:function(output){
console.log(output);
$(this.el).append("<button id='add'>hiii</button>");
$(this.el).append("<button id='removeAll'>Remove all list item</button>");
}
});
在这里,我试图捕获 output
变量中的响应值...但它出现“未定义”。我有什么错误的想法吗?
最佳答案
fetch
方法是异步的,因此 output
变量在您使用它时尚未被分配。尝试将 render
调用放入成功回调中:
var self = this;
stuff.fetch({
success: function (collection, response) {
console.log(response);
output=response;
self.render(output);
return response;
}
});
关于javascript - 获取结果未定义(Backbone.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12067606/