使用本地存储适配器将Emberjs模型转换为JSON

时间:2013-10-28 12:18:56

标签: json ember.js ember-data

在我的应用程序中,我想将我的模型转换为json,然后将此json发送到server.for适配器,我使用LSA

例如,我有这样的模型:

App.AxisModel = DS.Model.extend({
 uniqueName: DS.attr('string'),
 name: DS.attr('string'),
 hierarchyUniqueName: DS.attr('string'),
 type: DS.attr('string'), //row,column,filter
 isMeasure: DS.attr('boolean'),
 isActive: DS.attr('boolean'), //is added to one of type 
 orderId: DS.attr('number'),
 isAll:DS.attr('boolean'),
 sort:DS.attr('string'),
});

json的样本:

{
"OlapApp.AxisModel": {
    "records": {
        "htlbv": {
            "id": "htlbv",
            "uniqueName": "[Customers].[(All)]",
            "name": "(All)",
            "hierarchyUniqueName": "[Customers]",
            "type": "column",
            "isMeasure": false,
            "isActive": true,
            "orderId": 0,
            "isAll": true,
            "sort": "none"
        },
        "t58i9": {
            "id": "t58i9",
            "uniqueName": "[Sellers].[(All)]",
            "name": "(All)",
            "hierarchyUniqueName": "[Sellers]",
            "type": "row",
            "isMeasure": false,
            "isActive": true,
            "orderId": 0,
            "isAll": true,
            "sort": "none"
        },
        "2t9lc": {
            "id": "2t9lc",
            "uniqueName": "[Cube1].[Cube1-1]",
            "name": "Cube1-1",
            "hierarchyUniqueName": "[Database].[Cube1]",
            "type": "filter",
            "isMeasure": true,
            "isActive": true,
            "orderId": 0,
            "isAll": false,
            "sort": "none"
         }
      }
 }
}

1 个答案:

答案 0 :(得分:1)

我看到两个选项

  1. 如果要与服务器通信,可以使用RESTAdapter而不是LocalStorage。它非常灵活,您可以覆盖模型特定的序列化程序(例如,App.Model将由App.ModelSerializer序列化)或覆盖一些默认值。

  2. 如果您有REST + LocalStorage的混合,并且您希望手动同步更改,则可以使用JSON.parse和JSON.stringify对其进行格式化,并使用jQuery与服务器通信并在本地同步更改。您可以查看以下答案,了解how to do this for create的示例。