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

Qll*_*lli 3 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();
});
Run Code Online (Sandbox Code Playgroud)

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

});
Run Code Online (Sandbox Code Playgroud)

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

...
var template = Handlebars.compile(Template_one)
var html = template(context)
template: html
...
Run Code Online (Sandbox Code Playgroud)

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

eig*_*ive 16

更新:仅使用Handlebars预编译器

Grunt之前提到的原因是因为它对于很多东西来说非常方便.因此,在我看来,了解/了解它非常重要.

但是你可以单独使用Handlebars预编译器实现完全相同的功能:

$ handlebars templates/* -f js/precompiled.handlebars.js
Run Code Online (Sandbox Code Playgroud)

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

原文:随着Grunt

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

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

project/
    assets/
        js/
        templates/
Run Code Online (Sandbox Code Playgroud)

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


安装Grunt

$ cd project/assets/
$ sudo npm install grunt --save-dev
Run Code Online (Sandbox Code Playgroud)

安装Handlebars插件

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

像这样安装:

$ sudo npm install grunt-contrib-handlebars --save-dev
Run Code Online (Sandbox Code Playgroud)

Gruntfile.js

备注:

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

创建文件:

$ touch Gruntfile.js
Run Code Online (Sandbox Code Playgroud)

然后复制/粘贴这个典型的,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');
};
Run Code Online (Sandbox Code Playgroud)

这里有所有插件选项.

运行任务

然后,假设您有模板assets/templates/,请运行:

$ grunt handlebars:compile
Run Code Online (Sandbox Code Playgroud)

如果一切正常,您应该能够在以下位置看到已编译的模板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"];

});
Run Code Online (Sandbox Code Playgroud)

与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);  
    });
});
Run Code Online (Sandbox Code Playgroud)