Ember Data仅填充“id”属性

时间:2012-05-23 14:05:25

标签: ember.js ember-data

问题:从服务器收到JSON,但只有id属性有值。 GUI仅显示字符串“2 1”,并忽略{{photoName}}。手动调用MyApp.PhotoController.get('content').objectAt(0).get('photoName')会返回undefined,而MyApp.PhotoController.get('content').objectAt(0).get('id')会返回正确的ID。

任何Sugggestions?

//我的模特:

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'
});

//我的StateManager

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

1 个答案:

答案 0 :(得分: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