3

问题:从服务器接收到 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

4

1 回答 1

1

啊..明白了..需要添加代码来对我的属性进行去骆驼化:

DS.Model.reopen({
    namingConvention: {
    keyToJSONKey: function(key) {
        return key;
    },

    foreignKey: function(key) {
        return key;
    }
  }
});

从以下页面找到了这个,我想我应该读过,无论如何:D https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md

于 2012-05-23T14:30:32.877 回答