1

我使用的backbone.layoutmanager項目: https://github.com/tbranyen/backbone.layoutmanager#readmebackbone.layoutmanager和車把的模板引擎

可有人請張貼與車把模板引擎的樣本?包含修改後的app.js文件和實例視圖的 ?

我遵循指示,我有點困惑我應該在實例級別和全局中做什麼。 我不斷收到「我的模板有沒有一種方法‘匹配’ERR消息

感謝

+0

發佈代碼出來看看,可能可以幫到你 – 2012-07-09 14:56:50

回答

5

你修改app.js會的東西像這樣工作:

define([ 
    "jquery", 
    "underscore", 
    "backbone", 
    "handlebars", 
    "plugins/backbone.layoutmanager" 
], 
function($, _, Backbone, Handlebars) { 
    "use strict"; 

    var JST = window.JST = window.JST || {}; 

    Backbone.LayoutManager.configure({ 
    paths: { 
     layout: "path/to/layouts/", 
     template: "path/to/templates/" 
    }, 

    fetch: function(path) { 
     path = path + ".html"; 

     if(!JST[path]) { 
     $.ajax({ url: "/" + path, async: false }).then(function(contents) { 
      JST[path] = Handlebars.compile(contents); 
     }); 
     } 

     return JST[path]; 
    } 

    // It is not necessary to override render() here. 
    }); 

    var app = { 
    // Define global resources here. 
    }; 

    return _.extend(app, Backbone.Events); 
}); 

的一個例子觀點:

var SampleView = Backbone.View.extend({ 
    template: "path/to/sample/template", 

    // Override this for fine grained control of the context used by the template. 
    serialize: function() { 
    return { 
     property: 1, 
     anotherProperty: 2 
    }; 
    } 

    // No need to override render() for simple templates. 
}); 

並與視圖相關聯的模板:

<div> 
    <h2>{{property}}</h2> 
    <h2>{{anotherProperty}}</h2> 
</div>