我没有使用 RESTAdapter,所以我创建了 Ember 对象并使用reopenClass 方法和 jquery ajax 函数进行 ajax 请求,代码是:
OlapApp.Dimenssions = Ember.Object.extend({});
OlapApp.Dimenssions.reopenClass({
measure:Ember.A(),
find:function(cubeUniqueName){
var c = Ember.A();
var xhr = $.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'http://localhost:9095/service.asmx/getDimension',
data: '{"cubeUniqueName":"'+cubeUniqueName+'"}',
success: function(response) {
var data = JSON.parse(response.d);
$.each(data.hierarchies,function(i,v) {
c.pushObject(OlapApp.Dimenssions.create(v));
});
console.log(c);
}
});
return c;
}
});
当OlapApp.Dimenssions.find()
调用服务器响应一个像这样的 json 时:
{
"uniqueName": "[Customers] = [Database].[Cube 2]",
"name": "Customers",
"caption": "Customers",
"dimensionUniqueName": null,
"description": "Description",
"levelUniqueName": null,
"hierarchyUniqueName": null,
"visible": true,
"hierarchies": [
{
"uniqueName": "[Customers]",
"name": "Customers",
"caption": "Customers",
"dimensionUniqueName": "[Customers]",
"levels": [
{
"uniqueName": "[Customers].[(All)]",
"name": "(All)",
"caption": "(All)",
"description": "Description",
"hierarchyUniqueName": "[Customers]",
"dimensionUniqueName": "[Customers]",
"visible": true
},
{
"uniqueName": "[Customers].[Country]",
"name": "Country",
"caption": "Country",
"description": "Description",
"hierarchyUniqueName": "[Customers]",
"dimensionUniqueName": "[Customers]",
"visible": true
}
]
}
]
}
第一个问题是我不能在c数组中正确推送 json,所以我尝试为此创建一个模型,模型代码是:
OlapApp.RootMembers = DS.Model.extend({
id:DS.attr("number"),
uniqueName:DS.attr('string'),
name:DS.attr('string'),
caption:DS.attr('string'),
dimensionUniqueName:DS.attr('string'),
description:DS.attr('string'),
levelUniqueName:DS.attr('string'),
hierarchyUniqueName:DS.attr('string')
});
OlapApp.Levels = DS.Model.extend({
id:DS.attr("number"),
uniqueName:DS.attr('string'),
name:DS.attr('string'),
caption:DS.attr('string'),
hierarchyUniqueName:DS.attr('string'),
dimensionUniqueName:DS.attr('string'),
visible:DS.attr('boolean'),
description:DS.attr('string')
});
OlapApp.Hierarchies = DS.Model.extend({
id:DS.attr("number"),
uniqueName:DS.attr('string'),
name:DS.attr('string'),
caption:DS.attr('string'),
dimensionUniqueName:DS.attr('string'),
levels:DS.hasMany(OlapApp.Levels,{embedded:always}),
rootMembers:DS.hasMany(OlapApp.RootMembers,{embedded:always})
});
你可以在这里看到返回表单服务器的真实 json
但我怎样才能将此 json 映射到这些模型?如果我不能映射到模型,我可以将嵌套的 json 映射到 Ember.Object。