将嵌套的json映射到余烬数据模型 [英] mapping nested json to ember-data model

查看:70
本文介绍了将嵌套的json映射到余烬数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用RESTAdapter,所以我创建了Ember Object并使用reopenClass方法和jquery ajax函数进行ajax请求,并且代码为:

I'm not using RESTAdapter so I create Ember Object and using reopenClass method and jquery ajax function for ajax requesting and the code is:

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:

when OlapApp.Dimenssions.find() called the server response a json like this:

{
"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,所以我尝试为此创建一个模型,模型代码为:

the first problem is here that I can't push json corectly in c arrary so I try to create a model for this and the model code is:

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映射到Ember.Object。

but how can I map this json to these model ? if I can't map to model can I map nested json to Ember.Object.

推荐答案

我可以解决此问题代码行:

i can fix my problem with this line of code :

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

这篇关于将嵌套的json映射到余烬数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆