2013-03-16 113 views
1

我USNG Ember.js 1.0.0 RC1和灰燼數據修訂12無法顯示燼數據

模型我的後背和Ember.js作爲UI PHP修身框架。我想從REST後端加載數據並將其列在模板中。

因此,這裏是我的代碼:

window.App = Ember.Application.create(); 

// Store 
App.Store = DS.Store.extend({ 
    revision: 12, 
}); 

// Adaper 
DS.RESTAdapter.reopen({ 
    url: '/slim' 
}); 

// Router 
App.Router = Ember.Router.extend(); 
App.Router.map(function(){ 
    this.route('ads', {path: '/ads'}); 
}); 

// Ad model 
App.Ad = DS.Model.extend({ 
    title: DS.attr('string') 
}); 

// AdsRoute 
App.AdsRoute = Ember.Route.extend({ 
    model: function(){ 
     return App.Ad.find(); 
    } 
}); 

現在,我嘗試從我的模板商店渲染我的模型:從後端

<script type="text/x-handlebars" data-template-name="ads"> 
<h1>Ads</h1> 
{{#each controller}} 
    {{title}} 
{{/each}} 
</script> 

響應:

{ads:[{title:"Title" },{ title:"other title" }]} 

但商店中沒有顯示任何內容。我的問題是我應該如何在控制器模板中使用控制器中的數據?

Thx for reading!

SOLUTION

我不得不添加各地的JSON響應

{"ads":[{ "title":"Title" },{ "title":"other title" }]} 
+0

這是一個棘手的錯誤,因爲控制檯中沒有錯誤消息。非常感謝。在JSON周圍添加引號解決了它! – 2013-07-18 19:38:33

回答

0

看起來,如果內容是永遠不會得到在控制器中設置的報價。 我懷疑如果你添加setupController掛鉤到你的App.AdsRoute它會把項目放入控制器。

setupController: (controller, model) -> 
     controller.set('content', model) 

我包括我正在使用的修訂版11應用程序的實際工作代碼。所以模型名稱不會排隊,但代碼正在工作。

App.ChecksIndexController = Ember.ArrayController.extend 
    content: [] 

App.ChecksIndexRoute = Ember.Route.extend 
    enter: -> 
    console?.log("Checks Index Route a") 
    model: -> 
    App.Check.find() 
    setupController: (controller, model) -> 
    @._super() 
    console?.log('route setupController') 
    console?.log(model) 
    controller.set('content', model) 


{{#each item in controller }} 
     {{#linkTo checks.show item }} 
      <div class="well well-small well-trim-border"> 
      <h4>{{item.description}}</h5> 
      </div> 
     {{/linkTo}} 
     {{/each}} 

我回來的模型 - 包括一個id。

{"checks":[{"id":"512e0c6b1769d0805700000b","description":"xyz"}]} 
+0

謝謝@nrion快速回答。根據文檔setupController設置'內容'自動模型,除非被覆蓋。不過,我改變了你的代碼。不幸的是它不起作用:(我很確定它是由於修訂版本12. RC1罰款11,但12不起作用!是否有任何有關ember-data?文檔或API什麼? – greg 2013-03-16 13:17:23

+0

如果您致電controller.set('content',App.Ad.find())直接在setupController鉤子中?我知道這不是你想在生產中做到這一點,而是爲了排除故障?目前還沒有試圖使用12版本,所以這可能是問題的一部分 – nrion 2013-03-16 13:24:54

+0

Thx再次爲你的時間,nrion。當我這樣做時,沒有什麼變化當我嘗試打印出內容時,我得到:「 greg 2013-03-16 13:35:04

0

一件事是嘗試輸出模板中的一個小寫的「稱號」,爲你的模型的屬性定義爲一個小寫的「T」開始。

+0

謝謝你回答米哈爾。我承認我的模板不夠清晰。我只想爲我的收藏中的每件物品打印任何東西。我編輯代碼來表達我的目標。 – greg 2013-03-16 13:23:21