Emberdata 1.0.0

≡放荡痞女 提交于 2019-12-11 05:31:03

问题


I'm using Laravel 4 with emberjs and I need to list a collection of relationship data on emberjs app, but L4 print collection a little bit different, and so, I am trying to change the REST serialization but no work for now.. My Ember Data version is 1.0.0-beta.7+canary.238bb5ce. somebody help?

My JSON DATA:

{
"names": [
{
    "id": "1",
    "description": "List 2014",
    "user_id": "3",
    "squares": [
    {
        "id": "1"
    }
    ]
}
],
"squares": [
{
    "id": "1",
    "name": "squa1",
    "role_id": "1"
    },
    {
        "id": "2",
        "name": "squa2",
        "role_id": "1"
    }
    ]}

My models.js:

App.NameSerializer = DS.RESTSerializer.extend({
     primaryKey: 'id',
    extractArray: function(store, type, payload, id, requestType) {
        var posts =payload.names;
        var squares = [];
        payload.names[0].description = payload.names[0].description+"!!!";
        the = payload;


        posts.forEach(function(post){
               var reporter = post.squares,
                   reporterId = reporter.id;

                squares.push(reporter);
                post.reporter = reporterId;
        });

        payload.squares = squares;

        return this._super(store, type, payload, id, requestType);
    }
});



App.Name = DS.Model.extend({
    description: DS.attr('string'),
    squares: DS.hasMany('square'),
});
App.Square = DS.Model.extend({
    name: DS.attr('string'),
});

回答1:


Really the only problem with the json for what you're doing so far is the array of square ids.

"squares": [ { "id": "1" } ]

Ember Data is expecting

"squares": [ "1" ]

This can easily be remedied down the path you were taking

App.NameSerializer = DS.RESTSerializer.extend({
    primaryKey: 'id', // note this isn't necessary, the primary key is id by default
    extractArray: function(store, type, payload, id, requestType) {
      payload.names.forEach(function(name){
        var validSquareIdArray = [];
        name.squares.forEach(function(square){
          validSquareIdArray.push(square.id);
        });
        name.squares = validSquareIdArray;
      });

        return this._super(store, type, payload, id, requestType);
   }
});

http://emberjs.jsbin.com/OxIDiVU/279/edit



来源:https://stackoverflow.com/questions/22366924/emberdata-1-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!