将嵌套的json映射到ember-data模型

时间:2013-09-15 10:32:38

标签: json ember.js ember-data

我没有使用RESTAdapter所以我创建了Ember Object并使用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 arrary中推出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})

});

你可以看到返回表单服务器here

的真实json

但是如何将这个json映射到这些模型?如果我无法映射到模型,我可以将嵌套的json映射到Ember.Object。

2 个答案:

答案 0 :(得分:0)

this.set('yourModelClass', yourArray.props);

这应该可以解决问题,因为你说你拥有数组'C'中的所有json数据

答案 1 :(得分:-1)

我可以用这行代码解决我的问题:

var data = JSON.parse(response.d);
c.pushObject(Ember.Object.create(data));