2013-02-26 14 views
2

任何人都可以幫助確定以下Ember.js/Require.js應用程序無法呈現索引視圖的原因嗎? Ember細分市場獨立運作。Ember - RequireJS

application.html

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8" /> 
    <script data-main="../script/application" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.4/require.min.js"></script> 
    </head> 
    <body> 
    <script type="text/x-handlebars-template" data-template-name="index"> 
     login page 
     <a href="#" {{action "login"}}>Login</a> 
    </script> 
    <script type="text/x-handlebars-template" data-template-name="application"> 
     {{outlet}} 
    </script> 
    </body> 
</html> 

的application.js

require(["library/jquery", "library/handlebars", "library/ember"], function() { 
    Application = Ember.Application.create({ 
     LOG_TRANSITIONS: true 
    }); 

    Application.IndexView = Ember.View.extend({}); 

    Application.IndexController = Ember.Controller.extend({ 
     login: function() { 
      console.log("called login controller method."); 
     } 
    }); 

    Application.Router.map(function() { 
     this.route("index", { 
      path: "/login" 
     }); 
    }); 
}); 

我已經證實了以下文件成功需要與加載沒有腳本錯誤顯示在Safari或Chrome瀏覽器。

  • application.html
    • 的application.js
    • ember.js
    • handlebars.js
    • 的jquery.js
    • require.min.js

版本信息

要求:2.1.4

的jQuery:v1.9.1的

燼:V1.0.0-RC.1

把手:1.0.0-rc.3

+0

我還發現requirejs很難與其他庫一起使用,反之亦然。這就是爲什麼我創建了一個使用起來更容易,並且使用角度進行測試的庫。底部有一個演示應用程序:gngeorgiev.github.io/Modulerr.js您也可以將所有腳本合併爲一個,無需依賴於Modulerr.js – 2014-08-10 21:32:15

回答

4

所以經過頭撞和夜晚基普的某個時間。我終於得到了一個Ember &的例子,要求玩的很好。發現有關的應用模塊/相關性,加上灰燼設置和DOM解析的定時裝載

https://github.com/ben-crowhurst/EmberRequireSetup

的幾個問題。

+1

我認爲有人可能擊敗了它。 https://github.com/zeflasher/ember-require-js – Blowsie 2013-09-03 15:56:43

2

問題可能是由於沒有將依賴關係傳遞給函數體。如果所有的依賴關係都在全局範圍內(並且requirejs的好處之一就是你完全可以不用擔心),那麼不這樣做只會有效。

試試這個:

require(["library/jquery", "library/handlebars", "library/ember"], 
    function($, handlebars, Ember) { 

    // verify that none of these is undefined 
    console.log($, handlebars, Ember); 

    var Application = Ember.Application.create({ 
     LOG_TRANSITIONS: true 
    }); 

    (...) 
}); 

如果任何打印到控制檯的變量仍然是undefined這是一個跡象,表明你的requirejs配置不正確。我強烈推薦Chrome的開發工具:您可以在函數體的第一行設置斷點,並在應用程序凍結時調查本地/閉包範圍。 「暫停未捕獲的異常」也有很大幫助。另外,避免在模塊中創建全局對象是一個好主意(這就是爲什麼我在應用程序變量中添加了var);模塊的目的之一是嚴格限制外面的「泄漏」。

最後,我不確信requirejs會與Handlebars代碼一起存儲在<script>標籤中。甚至沒有保證Handlebars將由時間瀏覽器加載application.html的部分加載。我從來沒有使用handlebars,但如果是這種情況,我敢肯定處理這個問題的正確方法已經在這個地方:)。