0
我试图加载车把模板,并通过延期对象/承诺渲染他们,但是当我通过把在deferreds重构代码,错误发生:骨干视图+递延车把模板载入
我的看法如下: :
var indexView = Backbone.View.extend({
initialize: function (options) {
this.options = options || {};
manager.getTemplate('path/to/template').then(function(tpl){
// tpl is a handlebar compiled template,returned by getTemplate
this.template = tpl;
this.render();
// that causes
// Uncaught TypeError: Object #<Object> has no method 'render'
// "this" is Backbone.View.extend.initialize
});
},
render: function(){
// this.options is undefined
this.$el.html(this.template(this.options.data));
return this;
}
});
我不明白我也应该为什么在渲染()函数this.options从。那么()函数达到this.render,也就是现在不确定 谢谢
@hiral谢谢你纠正我的语法,我没有注意到的错误 – Stormsson
让JavaScript的上下文的一些研究,这是一个有趣的话题值得了解的。此外,只需在异步调用之前将'this'放入一个var:'var self = this;'。然后在回调中使用'self'。 (事情是,你的匿名函数不是你的视图的方法,所以它不知道你的视图)我猜https://developer.mozilla.org/en-US/docs/Web/JavaScript/参考/运营商/这将是一个好地方开始:) – Loamhoof
@Stormsson欢迎:) – Hiral