0
我想使用带有下划线模板和骨干的辅助函数。 现在我做这种方式:骨干使用带有帮助程序的下划线模板
查看:
var View = Backbone.View.extend({
// ...
template: getTpl('#b_ezlo', 1),
// ...
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
模板消气: 在这里,我通常会返回模板,用辅助功能一起。 问题是,当我做prepare
我不能返回辅助功能,因为它需要还返回其他变量的模板,它会导致undefined getDisabledState
:模板
function getTpl(tpl, options) {
if (!tpl) return;
if (!options) options = null;
var prepare = false;
if (options == 1) {
// this is called on View initialization
// with template: getTpl('#b_ezlo', 1),
options = {};
prepare = true;
}
var viewHelpers = {}
if (tpl == "#b_view") {
console.log("prepare", prepare);
viewHelpers.getDisabledState = function() {
if (typeof options.disabled != "undefined") {
return options.disabled;
} else {
return '';
}
}
}
_.extend(options, viewHelpers);
if (prepare) {
return _.template($(tpl).html());
} else {
return _.template($(tpl).html())(options);
}
}
部分以及(玉),我想使用助手:
.icon-block(data-disabled!="<% if (typeof getDisabledState != 'undefined') {getDisabledState()} %>")
我不喜欢这里if (typeof getDisabledState != 'undefined')
的一部分,这是不不错的模板。
所以,如果有任何其他方式来准备与辅助功能模板?
我觉得你可以在MarionetteJS签同一个任务的实现。有templateHelpers功能非常接近您的任务。 – Evgeniy 2014-10-08 07:39:54
Backbone + Marionette + Handlebars架构如何?把手在'registerHelper'中有一个构建,你可以声明你的定制助手。 – 2014-10-08 08:09:15
谢谢,@Evgeniy。也许应该这样做,他们定义视图内的帮助者。 – 2014-10-08 08:10:53