gpt4 book ai didi

requirejs - Backbone .marionette + i18n + Handlebars

转载 作者:行者123 更新时间:2023-12-02 22:42:57 25 4
gpt4 key购买 nike

有人可以发布一个将这些库组合在一起的示例吗?包括 i18n 和 marionette 的处理程序。

谢谢

最佳答案

指向backbone.marionette模板来编译hendlebars。这可以在你的 main.js 上完成:

Backbone.Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) {

return Handlebars.compile(rawTemplate);
};

配置您的应用程序以使用 Handlebars 和 i18n:这可以在您的 config.js 上完成:

require.config({

// Initialize the application with the main application file
deps: ["main"],

paths: {

libs: "../assets/js/libs",
plugins: "../assets/js/plugins",

// Libraries
jquery: "../assets/js/libs/jquery",
underscore: "../assets/js/libs/lodash",
backbone: "../assets/js/libs/backbone",
marionette: "../assets/js/libs/backbone.marionette",
handlebars: "../assets/js/libs/handlebars",

//plugins
text : "../assets/js/plugins/text",
i18n : "../assets/js/plugins/i18n",

},

config: {
//Set the config for the i18n
//module ID
i18n: {
locale: 'fr-fr'
}
},

shim: {

marionette: {
deps: ['backbone'],
exports: 'Backbone.Marionette'
},

backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
},

handlebars: {
deps: [],
exports: "Handlebars"
}

}
});

在您的任何模块上使用它:

    define([

'jquery',
'underscore',
'backbone',
'marionette',
'handlebars',
'text!templates/template.html',
'i18n!nls/your_i18n_text'
],

function($, _, Backbone, Marionette, Handlebars, tmpl, msg) {

'use strict';

var mod = Backbone.Model.extend({

defaults: function() {
return {
feedUrl : "this is for test"
};
}

});

view = Backbone.Marionette.ItemView.extend({

template: Handlebars.compile(tmpl),

model: new mod(),

initialize: function() {

this.tmpl_data = msg;
if(msg && this.model)
this.tmpl_data = _.extend(this.model.toJSON(),msg);
},

render: function() {

var view = this;

$(this.el).html(this.template(view.tmpl_data));
return this;
}

});


});

这将获取模板+ i18n 文件并渲染

关于requirejs - Backbone .marionette + i18n + Handlebars ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com