3

I'm using Marionette with requirejs and I would also like to use precompiled handlebars templates. How does this work?

Here my current setup:

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);  
    });

});

in my app.js I can get evth. work just fine with non compiled templates, like this:

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

but how to do it right with compiled templates?

4

1 回答 1

16

更新:仅使用 Handlebars 预编译器

我前面提到的原因Grunt是因为它对很多事情都非常方便。因此,在我看来,了解/了解它非常重要。

但是您可以单独使用Handlebars 预编译器实现完全相同的效果:

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

您仍然必须集成precompiled.handlebars.js到您的 RequireJS 配置中,请参阅答案末尾。

原文:与咕噜声

为此,您将需要Grunt Task Runner,它使这类事情变得容易得多。

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

project/
    assets/
        js/
        templates/

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


安装咕噜

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

安装车把插件

您还需要 Handlebars Grunt 插件来预编译您的模板:

像这样安装它:

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

Gruntfile.js

备注

  • AGruntfile.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');
};

所有插件选项在这里

运行任务

然后,假设您有模板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);  
    });
});
于 2013-11-14T03:07:03.643 回答