[英]can't display models from ember-data
我正在使用Ember.js 1.0.0 RC1和ember-data版本12
我的背面有PHP Slim框架,而Ember.js作為UI。 我想從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" }]}
但是商店中什么也沒有顯示。 我的問題是我應該如何使用車把模板中控制器的數據?
感謝閱讀!
解
我必須在JSON響應周圍添加引號
{"ads":[{ "title":"Title" },{ "title":"other title" }]}
似乎從未在控制器中設置內容。 我懷疑是否將setupController鈎子添加到App.AdsRoute會將其放置到控制器中。
setupController: (controller, model) ->
controller.set('content', model)
我包括了當前正在使用的修訂版11 App中的實際工作代碼。 因此,模型名稱不會對齊,但是代碼可以正常工作。
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"}]}
要嘗試的一件事是在模板中輸出小寫的“標題”,因為您的模型將屬性定義為以小寫的“ t”開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.