2012-08-27 38 views
1

我得到EmberJS問題,我無法理解EmberJS無法找到視圖例外

問題可能在的jsfiddle發現: http://jsfiddle.net/wLKKQ/

JS:

var fileUploader = [] || fileUploader; 
fileUploader.app = Em.Application.create(); 

fileUploader.app.userDetailsView = Em.View.create({ 
    clientIP: null 
});​ 

HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
</head> 
<body> 
    <script type="text/x-handlebars"> 
     {{#view fileUploader.app.userDetailsView}} 
      <h2>Hello Guest, your IP is: {{clientIP}}</h2> 
     {{/view}} 
    </script> 
    <script> 
     $(function() { 
      fileUploader.app.userDetailsView.set('clientIP', '::1'); 
     }); 
    </script> 
</body> 
</html> 

我收到以下錯誤:

Uncaught Error: assertion failed: Unable to find view at path 'fileUploader.app.userDetailsView'

回答

2

你的代碼有幾個問題。

  • Ember.Application應該位於大寫的名稱空間中(請參閱The Emberist blog)。
  • 你必須通過一個視圖類的#view幫手,不是一個實例(你因此有Ember.View.extend更換Ember.View.create
  • 你的腳本編輯clientIP不起作用,因爲現在FileUploader.app.userDetailsView是一類,你可以。做同樣的事情與didInsertElement Ember.View方法
  • 在模板中,你必須指定view.clientIP,根據View Context Changes

現在this JSFiddle work

FileUploader = [] || fileUploader; 
FileUploader.app = Em.Application.create(); 
FileUploader.app.userDetailsView = Em.View.extend({ 
    clientIP: null, 

    didInsertElement: function() { 
     this.set("clientIP", "::1"); 
    } 
});​ 

的HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
</head> 
<body> 
    <script type="text/x-handlebars"> 
     {{#view FileUploader.app.userDetailsView}} 
     <h2>Hello Guest, your IP is: {{view.clientIP}}</h2> 
     {{/view}} 
    </script> 
</body> 
</html>