我的客户端API以下列格式返回JSON:
{
"Result": [
{
"member": {
"id": 1,
"name": "Mark Smith",
"email_address": "name@email.com",
"member_type": [
"Premium"
],
"privileges": [
"MEMBER"
]
},
"activites": {
"ActivityId": "15",
"ActivityName": "Tennie"
}
}
],
"ResultCount": 1
}
我需要映射到我的Ember app模型:
App.Member = DS.Model.extend({
memberid: DS.attr('number'),
membername: DS.attr('string'),
memberemail: DS.attr('string'),
membertype: DS.attr('string'),
memberprivileges: DS.attr('number'),
activityID: DS.attr('number')
});
App.MemberSerializer = DS.RESTSerializer.extend({
extractArray: function(store, type, payload, id, requestType){
...
我尝试使用Serializer来映射json和app模型之间的字段,但不知道如何使用extractArray这样做,因为我找不到任何有关如何操作的教程/示例,所以有人可以请根据我的客户端JSON结构向我提供了如何执行此操作的示例?
我根据詹姆斯的回复做出的补充:
App.ArrayTransform = DS.Transform.extend({
deserialize: function (serialized) {
return (Ember.typeOf(serialized) == "array")
? serialized
: []
},
serialize: function (deserialized) {
var type = Ember.typeOf(deserialized)
if (type == 'array') {
return deserialized
} else if (type == 'string') {
return deserialized.split(',').map(function (item) {
return jQuery.trim(item)
});
} else {
return []
}
}
})
App.register("transform:array", DS.ArrayTransform);
App.Member = DS.Model.extend({
cont : DS.attr('array')
});
答案 0 :(得分:0)
您需要为数组
制作和使用自己的DS.Model对象/**
* Configure a transform for DS.Model Arrays
* DS.attr('array')
*/
App.ArrayTransform = DS.Transform.extend({
deserialize: function (serialized) {
return (Ember.typeOf(serialized) == "array")
? serialized
: []
},
serialize: function (deserialized) {
var type = Ember.typeOf(deserialized)
if (type == 'array') {
return deserialized
} else if (type == 'string') {
return deserialized.split(',').map(function (item) {
return jQuery.trim(item)
});
} else {
return []
}
}
})
答案 1 :(得分:0)
试试这个:
App.Member = DS.Model.extend({
memberid: DS.attr('number'),
membername: DS.attr('string'),
memberemail: DS.attr('string'),
membertype: DS.attr('string'),
memberprivileges: DS.attr('number'),
activityID: DS.attr('number')
});
App.MemberSerializer = DS.RESTSerializer.extend({
attrs: {
memberid: 'id',
membername: 'name',
memberemail: 'email',
membertype: 'type',
memberprivileges: 'privileges',
activityID: 'ActivityId'
}
});