本文介绍了骨干浏览+递延车把模板载入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图加载车把模板,并通过延期对象/承诺使他们,但是当我通过把在deferreds重构了code,错误发生:
I am trying to load handlebars templates and rendering them via deferred objects / promises, but when i refactored the code by putting in deferreds , errors are occurring:
我的看法如下:
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,也就是现在不确定谢谢
i can't understand how i'm supposed to reach this.render from the .then() function and also why in the render() function this.options is now undefined Thank you
推荐答案谨防恶性这个!它可能不是你认为它是当你的延迟函数被调用,特别是在jQuery的参与。
Beware the malignant this! It may not be what you think it is when your deferred function is called, especially where jQuery is involved.
尝试
initialize: function (options) { this.options = options || {}; var myself = this; manager.getTemplate('path/to/template').then(function(tpl){ // tpl is a handlebar compiled template,returned by getTemplate myself.template = tpl; myself.render(); }); }更多推荐
骨干浏览+递延车把模板载入
发布评论