2012-11-19 70 views
5

我正在嘗試使用淘汰賽,需要,下劃線來構建一個小應用程序。敲除requirejs和下劃線來獲得外部模板

我有我的索引頁面,在這裏我呼籲在需要和它指向一個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) { 

}); 

我的索引其餘沒有主體。所以當它加載它調用app.js

define(['jquery','knockout', 'appViewModel'], 
function($, ko, appViewModel) 
{ 
    ko.applyBindings(new appViewModel()); 
}); 

這應該然後調用appViewModel工作正常。這是我有點困惑,我則想從appViewModel

在模板中加載

所以我試圖做這樣的事情

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

     //call and load in template 

}); 

這就是我有點卡住我知道骨幹比如我可以用

template:_.template(homeViewTemplate) 

但我真的不知道我已經看過https://github.com/ifandelse/Knockout.js-External-Template-Engine但在這裏

裝載模板的最佳方式不符合要求,如果你使用它沒有要求,只是把一些文本在HTML文件中,並在我使用jQuery手機時調用它不添加類等

我想知道是否有人可以指向我正確的方向..我想我真的試圖找出什麼代碼放在這裏

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

     //call and load in template 

}); 

要在homeviewtemplate中調用。

感謝

回答

2

我使用jQuery模板HTML插入頁面,然後申請我的淘汰賽綁定。

$('#selector').append(homeViewTemplate); 
ko.applyBindings(VIEWMODEL, $('#selector')[0]); 

您還可能有興趣在我的WIP文章關於advanced knockout binding

+0

非常有趣的文章。如果你能在github上提供這樣的代碼示例,那將是非常好的。 – Dan

+0

我正在嘗試使用jQueryMobile也 - 所以我想我將不得不在事件發生時,鏈接改變..在骨幹我會關閉路由,然後使用骨幹路由器..你會建議做在這裏類似的事情,因爲我將需要知道何時加載另一個頁面模板.. – Dan

+0

是的。這可能是最好的計劃。 KnockoutJS沒有配備路由器,但實現第三部分並不困難。我沒有太多的jQuery手機經驗,但它應該工作。 –