2013-05-30 38 views
2

我正在爲不符合規範的JSON數據實現自定義適配器。數據正在被拉入,但屬性沒有被實現。我有以下地圖:模型屬性爲null,即使映射後也是如此

// map function has not been overridden - RESTAdapter is super. 
DS.ArcGisAdapter.map("App.Cai", { 
    caiId: { key: "CAIID" }, 
    name: { key: "ANCHORNAME" } 
}); 

而對於我的主鍵:

App.restSerializer.configure("App.Cai", { 
    primaryKey: "CAIID" 
}); 

隨着App.Cai(模型)是這樣的:

var attr = DS.attr, 
    belongsTo = DS.belongsTo; 

App.Cai = DS.Model.extend({ 
    caiId: attr("string"), 
    name: attr("string") 
}); 

從我的模板,我得到這個數據(縮寫代表)通過{{debugger}} & {{log item}}

_data: { 
    attributes: { 
    caiId: null 
    name: null 
    }, 
    id: null 
}, 
id:"130012000149" 

正如您所看到的,該ID是在頂層進行的,但在其下方沒有映射到caiId,也沒有映射到name那裏。下面是我的定製適配器的findQuery功能:

findQuery: function(store, type, query, recordArray) { 
    var root = this.rootForType(type), 
    transformedJSON = {}, 
    adapter = this, 
    rejectionHandler = function (reason) { 
     Ember.Logger.error(reason, reason.message); 
     throw reason; 
    }; 

    return this.ajax(this.buildURL(root), "GET", { 
    data: query 
    }).then(function(json){ 
    var feature, 
     index = 0; 

    root = root + "s"; 
    transformedJSON[root] = []; 

    for(;index < json.features.length; index++) { 
     feature = json.features[index]; 
     transformedJSON[root].push(feature.attributes); 
    } 

    adapter.didFindQuery(store, type, transformedJSON, recordArray); 
    }).then(null, rejectionHandler); 
} 

歡迎任何想法!謝謝:) P.S.讓我知道你是否需要任何其他信息。

編輯Gist到JSON數據前/後轉換。

編輯2:我想出了一個黑客,但這並沒有解決這個問題。

我擴展了JSONSerializer並將其設置爲適配器的序列化程序。 我必須實現keyForAttributeName掛鉤,如果映射不返回任何內容,則該掛鉤是回退。這裏是我的實現:

keyForAttributeName: function (type, name) { 
    var attributes = (Ember.meta(DS.ArcGisAdapter, true)["DS.Mappable"] || {}).attributes, 
    guid = Ember.guidFor(type.toString()), 
    result = name; 

    if (attributes && guid) { 
    result = attributes.values[guid][name].key; 
    } 

    return result; 
} 

仍然在尋找任何幫助,爲什麼我的模型缺少地圖。

+0

你的模型App.Cai看起來是怎樣的? – intuitivepixel

+0

@intuitivepixel我已更新我的問題以反映模型。 – knownasilya

+0

仍在尋找一些輸入。謝謝! – knownasilya

回答

0

我想你有一個模型id衝突在你的映射。在ember-data定義模型時,如果您的API respnse JSON的id字段被稱爲id,則根本不需要定義id字段。所以我認爲你的模型定義和你的映射有些不對。

嘗試定義以下內容,但不包括id,因爲這將通過覆蓋primaryKey進行定義。因此,這讓我們有以下幾點:

你映射

DS.ArcGisAdapter.map("App.Cai", { 
    name: { key: "ANCHORNAME" } 
}); 

模型:

var attr = DS.attr; 
App.Cai = DS.Model.extend({ 
    name: attr("string") 
}); 

和適配器primaryKey覆蓋:

App.restSerializer.configure("App.Cai", { 
    primaryKey: "CAIID" 
}); 

這樣您CAIID也就那麼自動設置爲id fi你的模型的領域。

讓我知道它是否有助於解決您的問題。

+0

不幸的是,這並沒有幫助,'caiId'從'_data'中消失了,但'name'仍然是'null'。頂層的「ID」仍然正確。 – knownasilya

+0

你能展示你的原始JSON從後端回來的樣子嗎? – intuitivepixel

+0

這是一個要點,顯示了我的原始有效載荷,並在上面的'findQuery'函數中進行了轉換:https://gist.github.com/knownasilya/5759147 – knownasilya

相關問題