2014-03-13 49 views
5

我使用的一個網站上淘汰賽require.js和想使用此鏈接http://knockoutjs.com/examples/grid.html的simpleGrid例子不過,我可以不包括與kncokout.simpleGrid.3.0.js使用knockout.simpleGrid.3.0.js要求。與Require.js

我已經試過包裝插件與

define(['jQuery', 'knockout'], // Require knockout 
    function($, ko) { 

    }); 

這不起作用,看來問題出現與模板。

任何幫助表示讚賞

回答

0

在您需要的配置,你應該創建一個路徑simpleGrid庫,並使用墊片來告訴它取決於淘汰賽,使您的庫以正確的順序加載。這裏有一個例子:

var require = { 
    paths: { 
     'jquery': 'lib/vendor/jquery-2.0.3', 
     'ko': 'lib/vendor/knockout-3.0.0', 
     'koSimpleGrid': 'lib/vendor/knockout.simpleGrid.3.0' 
    }, 
    shim: { 
     'koSimpleGrid': { 
      deps: ['ko'] 
     }, 
    } 
}; 

然後你可以複製從例如粘貼視圖模型代碼內部的定義是這樣的:

define(['jquery', 'ko', 'koSimpleGrid'], function ($, ko) { 
    // VIEW MODEL GOES HERE 
}); 
0

我同意這個問題似乎是與編寫代碼網格模板。本質上,,因爲requirejs異步加載模塊,document.write()不能使用 - 在模塊執行時寫入文檔已完成This StackOverflow answer explains it well我想。

我繞着它通過代替創建和附加使用DOM方法所需的腳本標籤模板:

templateEngine.addTemplate = function(templateName, templateMarkup) { 
    var scriptTag = document.createElement('script'); 
    scriptTag.type = 'text/html'; 
    scriptTag.id = templateName; 
    scriptTag.text = templateMarkup; 
    document.getElementsByTagName('body')[0].appendChild(scriptTag); 
}; 

My amd version is in this gist