javascript – 使用Backbone.LayoutManager进行Dust.js模板渲染
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用Backbone.LayoutManager进行Dust.js模板渲染,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1730字,纯文字阅读大概需要3分钟。
内容图文
![javascript – 使用Backbone.LayoutManager进行Dust.js模板渲染](/upload/InfoBanner/zyjiaocheng/792/20b6d751875c4f4bb27f34cea4d3916f.jpg)
让我详细说明我的设置:
我有预编译的dust.js模板,我想与Backbone.LayoutManager一起使用.
Backbone.LayoutManager使用以下配置:
Backbone.LayoutManager.configure
manage: true
fetch: (name) ->
name
所以模板的名称通过fetch直接传递给渲染,我想像这样覆盖渲染:
render: (template, context) ->
done = @async
dust.render @template, context, (err, out) ->
throw err if err
done(out)
但是我不能用渲染来做这个(大概是因为LayoutManager作者假设模板渲染是同步的).
有谁知道我怎么能做到这一点?
解决方法:
您需要修改LayoutManger以处理模板的异步呈现.
其中一种方法是使用jQuery Deferred对象,其中对render的调用返回Deferred对象的实例,然后在调用Dust库时解析该对象.
然后,修改Backbone.LayoutManager以继续执行它想要的模板,直到Deferred对象已解析.
在渲染中:
render: (template, context) ->
dfd = $.Deferred()
dust.render @template, context, (err, out) ->
throw err if err
done(out)
dfd.resolve()
return dfd;
然后,您可以在Backbone.LayoutManager中调用render,而不是使用render的返回来执行某些操作,您可以将“something”附加到已传递的延迟的done处理程序.
dfd = render(template, context)
dfd.done(do_something_with_this_template);
我正在使用Marionette.js(以及它的异步变体)和灰尘,这是我的核心渲染方法的样子:
render: function(){
var dfd = $.Deferred();
var template_context = this.model;
if(_.isUndefined(template_context) || _.isFunction(template_context)){
template_context = new (Backbone.Model.extend({}));
}
if(this.id) this.$el.attr('id', this.id);
var that = this;
dust.stream(this.tpl_name, template_context.toJSON())
.on('data', function(data){
that.$el.html(data);
})
.on('end', function(){
dfd.resolve();
that.enable_menus();
that.trigger('template:rendered');
})
.on('error', function(err){
dfd.reject();
window.le(err.message, err);
that.trigger('template:error');
});
return dfd;
}
(显然不使用coffeescript ……)
内容总结
以上是互联网集市为您收集整理的javascript – 使用Backbone.LayoutManager进行Dust.js模板渲染全部内容,希望文章能够帮你解决javascript – 使用Backbone.LayoutManager进行Dust.js模板渲染所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。