我正在爲不符合規範的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;
}
仍然在尋找任何幫助,爲什麼我的模型缺少地圖。
你的模型App.Cai看起來是怎樣的? – intuitivepixel
@intuitivepixel我已更新我的問題以反映模型。 – knownasilya
仍在尋找一些輸入。謝謝! – knownasilya