问题:从服务器接收到 JSON,但只有id
属性有值。GUI 只显示字符串“2 1”,而{{photoName}}
忽略。手动调用MyApp.PhotoController.get('content').objectAt(0).get('photoName')
返回undefined
,而MyApp.PhotoController.get('content').objectAt(0).get('id')
返回正确的 ID。
有什么建议吗?
//我的模型:
MyApp.Photo = DS.Model.extend({
id: DS.attr('number'),
photoName: DS.attr('string'),
photoDescription: DS.attr('string'),
photoFullSizeURL: DS.attr('string'),
photoThumbnailURL: DS.attr('string')
});
MyApp.Photo.reopenClass({
url: 'photos.json'
});
//我的状态管理器
MyApp.stateManager = Ember.StateManager.create({
rootElement: '#mainArea',
initialState: 'showMainView',
showMainView: Ember.ViewState.create({
enter: function(stateManager) {
this._super(stateManager);
var photos = MyApp.store.findAll(MyApp.Photo);
MyApp.PhotosController.set('content', photos);
},
view: Em.ContainerView.create({
childViews: ['photoListView'],
photoListView: Em.View.extend({
elementId: 'photoList',
templateName: 'photo-list-view',
contentBinding: 'MyApp.PhotosController.content'
})
})
})
})
//我的控制器:
MyApp.PhotosController = Ember.ArrayProxy.create({
content: []
});
//我的模板:
<script type="text/x-handlebars" data-template-name="photo-list-view">
PHOTOS:<br/>
{{#each content}}
{{photoName}} {{id}}
{{/each}}
</script>
//从服务器接收的JSON:
[
{
"id": 2,
"photoName": "Bird Photo",
"photoDescription": "Bird Photo Description",
"photoFullSizeUrl": "photos/bird.jpg",
"photoThumbnailUrl": "photos/bird_thumb.png"
},
{
"id": 1,
"photoName": "Bird Photo 2",
"photoDescription": "Bird Photo Description 2",
"photoFullSizeUrl": "photos/bird.jpg",
"photoThumbnailUrl": "photos/bird_thumb.png"
}
]
该代码也作为要点发布在这里:https ://gist.github.com/2775283