本文介绍了Emberdata 1.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Laravel 4与emberjs一起使用,我需要在emberjs应用程序上列出关系数据的集合,但是L4打印集合略有不同,因此,我正在尝试更改REST序列化,但目前无用..我的Ember Data版本是1.0.0-beta.7 + canary.238bb5ce.有人帮忙吗?

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?

我的JSON数据:

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

我的models.js:

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

推荐答案

到目前为止,json唯一的问题是正方形的id s数组.

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

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

预期的Ember数据

"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

这篇关于Emberdata 1.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 20:49