【问题标题】:Ember Data only populating the "id" propertyEmber Data 仅填充“id”属性
【发布时间】:2012-05-23 14:05:25
【问题描述】:

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

【问题讨论】:

    标签: ember.js ember-data


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 2014-07-09
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多