Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Backbone.Marionette视图中最轻松地呈现i18n文本和模型属性(使用把手模板)_Javascript_Jquery_Backbone.js_Marionette - Fatal编程技术网

Javascript 如何在Backbone.Marionette视图中最轻松地呈现i18n文本和模型属性(使用把手模板)

Javascript 如何在Backbone.Marionette视图中最轻松地呈现i18n文本和模型属性(使用把手模板),javascript,jquery,backbone.js,marionette,Javascript,Jquery,Backbone.js,Marionette,当模板中显示的数据是国际化文本或模型属性时,渲染模板是小菜一碟,但是当涉及到在一个模板中同时渲染这两个属性时,我似乎找不到一个干净的解决方案 作为参考,我通过Require.js的i18n插件使用i18n 假设我有一个简单的模板: <h3>{{displayText.load}} #{{id}}</h3> <h4 id="loading-load-data">{{displayText.loadingLoadData}}...</h4> 但是,

当模板中显示的数据是国际化文本或模型属性时,渲染模板是小菜一碟,但是当涉及到在一个模板中同时渲染这两个属性时,我似乎找不到一个干净的解决方案

作为参考,我通过Require.js的i18n插件使用i18n

假设我有一个简单的模板:

<h3>{{displayText.load}} #{{id}}</h3>

<h4 id="loading-load-data">{{displayText.loadingLoadData}}...</h4>
但是,一旦我想同时显示模型数据,这就不再有效了,主要原因是
this
在模板属性中未定义(因此我无法引用
this.Model.attributes
),而且我似乎不得不回过头来覆盖
render()
方法,将i18n对象和模型属性传递给模板,如下所示:

return Backbone.Marionette.ItemView.extend({
    template: Handlebars.compileClean(template),
    render: function() {
        var templateParams = _.extend({}, this.model.attributes, localizedText),
            renderedTemplate = this.template(templateParams);

        this.$el.html(renderedTemplate);

        this.bindUIElements();
        this.delegateEvents();

        return this;
    }
});
我真的很想保留木偶网对
render()
的默认处理,只使用template属性来呈现i18n文本和模型数据。这可能吗

奖金:假设我必须重写
render()
,我注意到在这样做时,木偶视图上提供的
this.ui
属性不再将每个项包装为jQuery对象。这意味着:

this.ui.loadingNotification.show();
停止运行,抛出一个
未捕获类型错误:对象#加载数据没有方法“show”
。这是为什么?我如何恢复正确的
this.ui
jQuery包装功能


编辑:解决了奖金;只需在
render()
方法中加入
this.binduiements()
调用,即可将元素正确绑定到
ui
哈希。请参见上面的
render()
示例。

已解决:因此答案非常简单。事实证明,当用作函数时,可以将参数传递到template:属性中,此参数表示与该视图/模板关联的模型:

<h3>{{displayText.load}} #{{id}}</h3>

<h4 id="loading-load-data">{{displayText.loadingLoadData}}...</h4>
template: function (model) {
    var templateParams = _.extend({}, model, localizedText),
        renderedTemplate = Handlebars.compileClean(template);

    return renderedTemplate(templateParams);
},
然后不再需要覆盖
render()
方法,i18n文本和模型数据都可以按预期呈现到模板中