我在這裏找到了this example這裏介紹瞭如何使用HBS插件來管理模板。這似乎是一個很好的解決方案。 @machineghost建議使用RequireJS包含如下模板:骨幹網模板:使用HBS動態切換模板
define(['template!path/to/someTemplate'], function(someTemplate) {
var MyNewView = BaseView.extend({template: someTemplate});
$('body').append(new MyNewView().render().el);
}
這很好,除了我需要動態切換模板。這裏是我的一個觀點的例子:
define([
'jquery',
'underscore',
'backbone',
'models/tableModel',
'collections/tablesCollection',
'views/tablesView'
], function($, _, Backbone, tableModel, tablesCollection, tablesView) {
var t = new tablesCollection(null, {url: 'applications-lab'});
return new tablesView({ collection: t, template: 'applications-lab-template', url: 'applications-lab'});
});
正如你所看到的,我在渲染視圖時傳遞了模板。我想知道的是,我可以傳遞一個變量給define
聲明,它會告訴Backbone要使用哪個模板路徑?我是Backbone的新手,特別是RequireJS,我不確定。建議任何人?
我的意思是先定義上述說法。 – sehummel