2013-12-16 29 views
0

我有一個兩個模型,用戶和主題,這兩個模型都依賴api數據的應用程序應用程序。該JSON返回不在該燼期望的格式,所以我對每一個模型 -Ember.js加載路線時出錯

var foo = { 

    'App.Subject': {'single':'subject','array':'subjects'} 

}; 

App.SubjectSerializer = DS.RESTSerializer.extend({ 
    extractSingle: function(store, type, payload, id, requestType) { 
    var p = {}; 
    p[foo[type]['single']] = payload; 
    return this._super(store, type, p, id, requestType); 
    }, 
    extractArray: function(store, type, payload, id, requestType) { 
    var p = {}; 
    p[foo[type]['array']] = payload; 
    return this._super(store, type, p, id, requestType); 
    }, 
    serializeIntoHash: function(hash, type, record, options) { 
    Ember.merge(hash, this.serialize(record, options)); 
    } 
}); 

自定義序列這完美的作品。但是,我有我的用戶模型 -

var foo = { 

    'App.User': {'single':'user','array':'users'} 

}; 

App.UserSerializer = DS.RESTSerializer.extend({ 
    extractSingle: function(store, type, payload, id, requestType) { 
    var p = {}; 
    p[foo[type]['single']] = payload; 
    return this._super(store, type, p, id, requestType); 
    }, 
    extractArray: function(store, type, payload, id, requestType) { 
    var p = {}; 
    p[foo[type]['array']] = payload; 
    return this._super(store, type, p, id, requestType); 
    }, 
    serializeIntoHash: function(hash, type, record, options) { 
    Ember.merge(hash, this.serialize(record, options)); 
    } 
}); 

當我嘗試訪問我的用戶在瀏覽器中,我得到這個錯誤了類似的串行:

Assertion failed: Error while loading route: TypeError: Cannot read property 'array' of undefined

誰能幫助?

編輯:在extractArray功能「型」指的是JSON的root屬性,例如,如果我的JSON是

{ 
    "user": { 
    name: "xyz", 
    email: "[email protected]" 
    } 
} 

的「類型」是「用戶」。

+0

並在extractArray功能是什麼 '類型' 時,代碼正在執行?,放置一個調試器或一個日誌,看看它有什麼價值。 – fanta

回答

1

mappings[type]未定義。什麼是映射?

順便說一句,還有一個更簡單的方法來做到這一點,

p[type.typeKey] = payload; 

陣列

p[Ember.String.pluralize(type.typeKey)] = payload; 
+0

應該是foo,看編輯 – bookthief

+1

當然,我告訴他他的錯誤在哪裏。我問他一個額外的問題,打30個字符。 – Kingpin2k

+0

不錯,現在有用,歡呼! – bookthief