使用预编译的车把模板与Marionette

时间:2013-11-08 15:56:47

标签: requirejs handlebars.js marionette

我正在使用带有requirejs的Marionette,我也想使用预编译的把手模板。 这是如何工作的?

这是我当前的设置:

require_main.js:

requirejs.config({
    baseUrl: "/",
    paths: {
        'text': 'vendor/javascripts/text',
        'backbone': "vendor/javascripts/backbone",
        'backbone.wreqr': "vendor/javascripts/backbone.wreqr",
        'backbone.babysitter': "vendor/javascripts/backbone.babysitter",
        'jquery': "vendor/javascripts/jquery",
        'jquery-ui': 'vendor/javascripts/jquery-ui',
        'json2': "vendor/javascripts/json2",
        'marionette': "vendor/javascripts/backbone.marionette",
        'underscore': "vendor/javascripts/underscore",
        'handlebars': "vendor/javascripts/handlebars"
    },

    shim: {
        'underscore': {
            exports: "_"
        },
        'backbone': {
            deps: ["jquery", "underscore", "json2"],
            exports: "Backbone"
        },
        'marionette': {
            deps: ["backbone"],
            exports: "Marionette"
        },
        'jquery-ui': ['jquery'],

    'handlebars': {
      exports: 'Handlebars'
    }
    }
});
require(["app"], function(MyApp){
    MyApp.start();
});

app.js:

define(['marionette', 'handlebars', 'text!compiled.handlebars'], function(Marionette, Handlebars, Template_one) {

    var MyApp = new Marionette.Application();

    MyApp.addRegions({
        mainRegion: "#main-region"
    });

    MyApp.StaticView = Marionette.ItemView.extend({
        template: Template_one(context)
    });

    MyApp.on("initialize:after", function(){
        var staticView = new MyApp.StaticView();
        MyApp.mainRegion.show(staticView);  
    });

});

在我的app.js中我可以得到evth。使用非编译模板可以正常工作,如下所示:

...
var template = Handlebars.compile(Template_one)
var html = template(context)
template: html
...

但是如何使用已编译的模板做到这一点?

1 个答案:

答案 0 :(得分:16)

更新:仅使用Handlebars预编译器

之前我提到Grunt的原因是因为LOT of things非常方便。因此,在我看来,了解/了解它非常重要。

但是你可以单独使用Handlebars precompiler完成同样的事情:

$ handlebars templates/* -f js/precompiled.handlebars.js

您仍然必须在您的RequireJS配置中集成precompiled.handlebars.js,请参阅答案的最后部分。

原文:With Grunt

你需要Grunt Task Runner,这使得这些事情变得更容易。

从现在开始,我假设以下文件夹结构:

project/
    assets/
        js/
        templates/

我还假设你的机器上安装了node.js


安装Grunt

$ cd project/assets/
$ sudo npm install grunt --save-dev

安装Handlebars插件

您还需要Handlebars Grunt plugin才能预编译模板:

像这样安装:

$ sudo npm install grunt-contrib-handlebars --save-dev

Gruntfile.js

备注

  • Gruntfile.js是Grunt的配置文件
  • 它位于安装Grunt CLI实例的位置的根目录
  • 用简单的javascript写的

创建文件:

$ touch Gruntfile.js

然后复制/粘贴这个典型的Gruntfile以完成您想要实现的目标:

module.exports = function(grunt) {

  /*
   * https://github.com/gruntjs/grunt/wiki/Configuring-tasks
   */
  grunt.initConfig({

    "handlebars": {
      compile: {
        options: {
          amd: true
        },
        src: ["templates/**/*.html"],
        dest: "js/precompiled.handlebars.js"
      }
    }

  });

  // Requires the needed plugin
  grunt.loadNpmTasks('grunt-contrib-handlebars');
};

所有插件选项here

运行任务

然后,假设您的模板位于assets/templates/,请运行:

$ grunt handlebars:compile

如果一切正常,您应该能够在js/precompiled.handlebars.js中看到已编译的模板:

define([

    // Should be `handlebars.runtime.js` here
    // See: http://handlebarsjs.com/precompilation.html
    'handlebars'

], function(Handlebars) {

  this["JST"] = this["JST"] || {};

  this["JST"]["templates/template_one.html"] = Handlebars.template(function(Handlebars,depth0,helpers,partials,data) { /* ... */ });

  this["JST"]["templates/template_two.html"] = Handlebars.template(function(Handlebars,depth0,helpers,partials,data) { /* ... */ });

  //...

  return this["JST"];

});

与RequireJS集成

显然,在你的Views中,你必须改变你的依赖关系:

define([
  'marionette',
  //'handlebars', /* You don't need this _here_ anymore */
  'js/compiled.handlebars'

], function(Marionette, JST) {

    /* edited for brevity */

    MyApp.StaticView = Marionette.ItemView.extend({
        template: JST["templates/template_one.html"]
    });

    MyApp.on("initialize:after", function(){
        var staticView = new MyApp.StaticView();
        MyApp.mainRegion.show(staticView);  
    });
});
相关问题