是否可以将 questions 变量传递到 View 渲染中?
我尝试在获取成功时调用 this.render 但出现错误,大概是因为 this.render 。不在正确的范围内。
app.AppView = Backbone.View.extend({
initialize: function(){
var inputs = new app.Form();
inputs.fetch({
success: function() {
var questions = inputs.get(0).toJSON().Questions;
app.validate = new Validate(questions);
app.validate.questions();
}, // End Success()
error: function(err){
console.log("Couldn't GET the service " + err);
}
}); // End Input.fetch()
this.render();
}, // End Initialize
render: function(){
el: $('#finder')
var template = _.template( $("#form_template").html(), {} );
this.$el.html(template);
}
最佳答案
使用与 View 实例不同的 this 对象调用成功回调。 解决这个问题的最简单方法是在调用 inputs.fetch 之前添加类似的内容:
var self = this;
然后在成功回调中:
self.render();
关于javascript - 将数据传递到渲染 Backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807751/