Ember数据仅填充“id”属性 [英] Ember Data only populating the "id" property

查看:116
本文介绍了Ember数据仅填充“id”属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:从服务器收到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'
});

//我的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。照片);
MyApp.PhotosController.set('content',photos);
},

视图:Em.ContainerView.create({
childViews: 'photoListView'],

photoListView:Em.View.extend({
elementId:'photoList',
templateName:'photo-list-view',
contentBinding:'MyApp.PhotosController.content'
})
})
})
})

//我的控制器:



< pre class =lang-js prettyprint-override> MyApp.PhotosController = Ember.ArrayProxy.create({
content:[]
});

//我的模板:

 < script type =text / x-handlebarsdata-template-name =photo-list-view> 
照片:< br />
{{#each content}}
{{photoName}} {{id}}
{{/ each}}
< / script>

// JSON从服务器收到:

  [
{
id:2,
photoName:鸟照,
photoDescription:鸟照片说明,
photoFullSizeUrl:photos / bird.jpg,
photoThumbnailUrl:photos / bird_thumb.png
},
{
id:1,
photoName:鸟照2,
photoDescription:鸟照相说明2,
photoFullSizeUrl照片/ bird.jpg,
photoThumbnailUrl:照片/ bird_thumb.png
}
]

代码也作为Gist发布: https:// gist。 github.com/2775283

解决方案

啊..得到了..需要添加代码去替换我的属性:

  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


Problem: The JSON is received from the server, but only the id property have a value. The GUI only displays the String "2 1", and the {{photoName}} is ignored. Manually calling MyApp.PhotoController.get('content').objectAt(0).get('photoName') returns undefined, whereas MyApp.PhotoController.get('content').objectAt(0).get('id') returns the correct ID.

Any Sugggestions ?

//My Model:

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

//My 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'
            })
        })
    })
})

//My Controller:

MyApp.PhotosController = Ember.ArrayProxy.create({
    content: []
});

//My template:

<script type="text/x-handlebars" data-template-name="photo-list-view">
        PHOTOS:<br/>
        {{#each content}} 
            {{photoName}} {{id}}
        {{/each}}
</script>

//JSON Received from server:

[
    {
        "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"
    }
]

The code is also posted as a Gist here: https://gist.github.com/2775283

解决方案

Ah.. Got it.. Needed to add code to de-camelize my properties:

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

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

Found this from the following page, which I guess I should have read, anyways :D https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md

这篇关于Ember数据仅填充“id”属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆