我正在尝试 Sails.JS 和 Ember.JS。
不幸的是,在这种情况下,Ember 期望 json 位于 http://jsonapi.org 中格式。 Sails.js JSON 如下所示:
// specific game
// /api/game/1
{
cards: [
{
id: 2,
createdAt: "2014-04-10T13:15:47.259Z",
updatedAt: "2014-04-10T13:15:47.259Z",
game: 1,
player: 1
}
],
table: {
id: 1,
createdAt: "2014-04-10T10:47:27.292Z",
updatedAt: "2014-04-10T10:47:27.292Z"
},
serverSeed: "test",
createdAt: "2014-04-10T13:15:03.872Z",
updatedAt: "2014-04-10T13:15:03.872Z",
id: 1
}
// all games
// api/game
[
{
cards: [
{
id: 2,
createdAt: "2014-04-10T13:15:47.259Z",
updatedAt: "2014-04-10T13:15:47.259Z",
game: 1,
player: 1
}
],
table: {
id: 1,
createdAt: "2014-04-10T10:47:27.292Z",
updatedAt: "2014-04-10T10:47:27.292Z"
},
serverSeed: "test",
createdAt: "2014-04-10T13:15:03.872Z",
updatedAt: "2014-04-10T13:15:03.872Z",
id: 1
}
]
是否有任何好方法可以转换 Sails.js JSON 或 Ember.js 序列化器来解决此问题?
最佳答案
我更改了 http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/ 上的代码一点点,现在可以工作了。
App.ApplicationSerializer = DS.RESTSerializer.extend({
/**
The current ID index of generated IDs
@property
@private
*/
_generatedIds: 0,
/**
Sideload a JSON object to the payload
@method sideloadItem
@param {Object} payload JSON object representing the payload
@param {subclass of DS.Model} type The DS.Model class of the item to be sideloaded
@param {Object} item JSON object representing the record to sideload to the payload
*/
sideloadItem: function(payload, type, item){
var sideloadKey = type.typeKey.pluralize(), // The key for the sideload array
sideloadArr = payload[sideloadKey] || [], // The sideload array for this item
primaryKey = Ember.get(this, 'primaryKey'), // the key to this record's ID
id = item[primaryKey];
// Missing an ID, generate one
if (typeof id == 'undefined') {
id = 'generated-'+ (++this._generatedIds);
item[primaryKey] = id;
}
// Don't add if already side loaded
if (sideloadArr.findBy("id", id) != undefined){
return payload;
}
// Add to sideloaded array
sideloadArr.push(item);
payload[sideloadKey] = sideloadArr;
return payload;
},
/**
Extract relationships from the payload and sideload them. This function recursively
walks down the JSON tree
@method sideloadItem
@param {Object} payload JSON object representing the payload
@paraam {Object} recordJSON JSON object representing the current record in the payload to look for relationships
@param {Object} recordType The DS.Model class of the record object
*/
extractRelationships: function(payload, recordJSON, recordType){
// Loop through each relationship in this record type
recordType.eachRelationship(function(key, relationship) {
var related = recordJSON[key], // The record at this relationship
type = relationship.type; // belongsTo or hasMany
if (related){
// One-to-one
if (relationship.kind == "belongsTo") {
// Sideload the object to the payload
this.sideloadItem(payload, type, related);
// Replace object with ID
recordJSON[key] = related.id;
// Find relationships in this record
this.extractRelationships(payload, related, type);
}
// Many
else if (relationship.kind == "hasMany") {
// Loop through each object
related.forEach(function(item, index){
// Sideload the object to the payload
this.sideloadItem(payload, type, item);
// Replace object with ID
related[index] = item.id;
// Find relationships in this record
this.extractRelationships(payload, item, type);
}, this);
}
}
}, this);
return payload;
},
/**
Overrided method
*/
extractArray: function(store, type, payload, id, requestType) {
var typeKey = type.typeKey,
typeKeyPlural = typeKey.pluralize(),
newPayload = {};
newPayload[typeKeyPlural] = payload;
payload = newPayload;
// Many items (findMany, findAll)
if (typeof payload[typeKeyPlural] != "undefined"){
payload[typeKeyPlural].forEach(function(item, index){
this.extractRelationships(payload, item, type);
}, this);
}
for(var key in payload) {
if(key === typeKeyPlural) {
for(var i =0; i < payload[key].length; i++) {
if(typeof payload[key][i] !== 'object') {
delete payload[key][i];
}
}
}
}
console.log(payload);
return this._super(store, type, payload, id, requestType);
},
extractSingle: function (store, type, payload, id, requestType) {
var typeKey = type.typeKey,
typeKeyPlural = typeKey.pluralize(),
newPayload = {};
if(!payload[typeKey]) {
newPayload[typeKey] = payload;
payload = newPayload;
if (typeof payload[typeKey] != "undefined"){
this.extractRelationships(payload, payload[typeKey], type);
delete payload[typeKeyPlural];
}
}
return this._super(store, type, payload, id, requestType);
}
});
关于javascript - Sails.js JSON 到 Ember,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989910/