backbone.marionette + I18N +车把(backbone.marionette

2019-07-30 10:54发布

有人可以发布这些库组合在一起的例子吗? 包括对国际化和提线木偶的处理程序。

谢谢

Answer 1:

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

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

        return Handlebars.compile(rawTemplate);
    };

配置您的应用程序使用的车把和国际化:这可以在你的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;  
    }

   });


});

这将获取模板+国际化文件和渲染



Answer 2:

我用国际化-JS ,这就是一切不可知的,所以你可以与任何服务器端框架(Ruby on Rails的我)和任何JavaScript模板引擎(Haml的咖啡对我来说)使用它。

下面是一个例子:

%form.form-horizontal
  .modal
    .modal-header
      %button{ class: 'close', data: { dismiss: 'modal' } } ×
      %h3
        = I18n.t(@property.get('name'), scope: 'data_sheets.properties')

    .modal-body
      - unless @property.get('editable')
        %p= I18n.t('data_sheets.you_already_contributed_to_this_property')

所以,没有什么办骨干也不木偶侧。



文章来源: backbone.marionette + i18n + handlebars