2
試圖削減代碼重複,我已經設置了$ firebaseArray擴展如下:
var listUsersFactory = $firebaseArray.$extend({
$$added: function (snap) {
return new Customer(snap);
},
$$updated: function (snap) {
var c = this.$getRecord(snap.key);
var updated = c.updated(snap);
return updated;
},
});
和客戶代碼:
function Customer(snap) {
this.$id = snap.key;
this.updated(snap);
}
Customer.prototype = {
updated: function(snap) {
var oldData = angular.extend({}, this.data);
this.data = snap.val();
// checks and such
}
}
這在加載,顯示和保存客戶列表時會產生奇蹟,我對此感到滿意。現在
,問題的關鍵在於獲取單個客戶和它的詳細信息頁面,因爲該客戶對象不是$fireObject
的延伸,因而缺乏一個$save
方法
單一客戶負荷:
customersRef.once("value", function(snapshot) {
if(snapshot.child(uuid).exists())
{
customersFactory.customerDetails = new Customer(snapshot.child(uuid));
return deferred.resolve();
}
}
但是當我打電話給customersFactory.customerDetails.$save()
時出現錯誤
如何擴展我的類以使其適用於數組和單對象用途?