2014-03-13 63 views
1

我正在使用Laravel 4與emberjs,我需要列出關於emberjs應用程序的關係數據的集合,但L4打印集合有點不同,所以,我試圖改變REST序列化但目前沒有工作.. 我的Ember數據版本是1.0.0-beta.7 + canary.238bb5ce。 有人幫忙?Emberdata 1.0.0

我的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:

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

回答

0

真正使用JSON迄今,你在做什麼,唯一的問題是正方形id S的陣列。

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

Ember公司的數據預計

"squares": [ "1" ] 

這可以很容易地糾正下你服用

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

+0

它的工作,現在的道路!謝謝! – Fernando