使用requirejs和下划线进行淘汰以获得外部模板

Dan*_*Dan 5 underscore.js knockout.js

我正在尝试使用knockout,require,下划线构建一个小应用程序.

我有我的索引页面,我在require中调用它指向一个main.js我保持我的配置

require.config({

paths: {
    jquery:     'vendor/jqm/jquery_1.7_min',
    knockout: 'vendor/knockout/knockout-2.2.0',
    underscore : 'vendor/underscore/underscore_amd',
    text:       'vendor/require/text',
    templates:  '../templates'
}

});

define(['app'], function(app) {

});
Run Code Online (Sandbox Code Playgroud)

我索引的其余部分没有正文.所以加载它时会调用app.js

define(['jquery','knockout', 'appViewModel'],
 function($, ko, appViewModel) 
{
    ko.applyBindings(new appViewModel());
});
Run Code Online (Sandbox Code Playgroud)

这应该调用appViewModel,它可以正常工作.这是我有点困惑,因为我想从appViewModel加载模板

所以我想做这样的事情

define(['jquery','knockout', 'text!templates/homeViewTemplate.html', 'jqm'],
function($, ko, homeViewTemplate) {

      //call and load in template

});
Run Code Online (Sandbox Code Playgroud)

这是我在骨干中有点卡住的地方,例如我可以使用

  template:_.template(homeViewTemplate)
Run Code Online (Sandbox Code Playgroud)

但我真的不确定在这里加载模板的最佳方法

我查看了https://github.com/ifandelse/Knockout.js-External-Template-Engine,但是这对require不起作用,如果你在没有require的情况下使用它,只需在html文件中放入一些文本并调用在我使用jQuery mobile时,它不会添加类等.

我想知道是否有人能指出我正确的方向..我想我真的想弄清楚要放在这里的代码

    define(['jquery','knockout', 'text!templates/homeViewTemplate.html', 'jqm'],
function($, ko, homeViewTemplate) {

      //call and load in template

});
Run Code Online (Sandbox Code Playgroud)

在homeviewtemplate打电话.

谢谢

Mic*_*pas 2

我使用 jQuery 将模板 HTML 插入页面,然后应用我的 Knockout 绑定。

$('#selector').append(homeViewTemplate);
ko.applyBindings(VIEWMODEL, $('#selector')[0]);
Run Code Online (Sandbox Code Playgroud)

您可能还对我关于高级敲除绑定的WIP 文章感兴趣。