backbone.marionette + i18n +车把

时间:2012-07-16 09:26:47

标签: requirejs handlebars.js marionette

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

由于

2 个答案:

答案 0 :(得分:12)

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

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

        return Handlebars.compile(rawTemplate);
    };

将您的应用配置为使用把手和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文件并呈现

答案 1 :(得分:2)

我使用i18n-JS,这是一切不可知的,所以你可以将它用于任何服务器端框架(Ruby on Rails)和任何Javascript模板引擎(Haml Coffee for me)。

以下是一个例子:

%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方面无关。