2016-11-07 94 views
-1

我想在我的Firebase數據庫中添加包含屬性前綴爲_的對象。添加帶有下劃線前綴屬性的對象

保存時似乎只有這些屬性被忽略。

我的代碼看起來是這樣的,並且工作正常:

.config(function($provide) { 
    $provide.decorator('$firebaseArray', function($delegate, $window) { 
     var add, timestamp, currentUser; 

     add = $delegate.prototype.$add; 
     timestamp = $window.firebase.database.ServerValue.TIMESTAMP; 
     currentUser = $window.firebase.auth().currentUser.uid; 

     $delegate.prototype.$add = function (newData) { 
      //works if remove '_' 
      newData['_createdAt'] = timestamp; 
      newData['_createdBy'] = currentUser; 

      return add.call(this, newData); 
     }; 

     return $delegate; 
    }); 
}) 

.config(function($provide) { 
    $provide.decorator('$firebaseObject', function($delegate, $window) { 
     var save, timestamp, currentUser; 

     save = $delegate.prototype.$save; 

     timestamp = $window.firebase.database.ServerValue.TIMESTAMP; 
     currentUser = $window.firebase.auth().currentUser.uid; 

     $delegate.prototype.$save = function() { 
      //works if remove '_' 
      this['_modifiedAt'] = timestamp; 
      this['_modifiedBy'] = currentUser; 

      return save.call(this); 
     }; 

     return $delegate; 
    }); 
}) 
+0

@cartant謝謝,我沒有發現這個問題。發現點不受Firebase支持,但下劃線沒有限制。我將編輯並保留我的問題。 –

+0

是的,下劃線應該是有效的。這些值是否顯示在Firebase控制檯中? – cartant

+1

@cartant只有當我刪除下劃線。我剛剛發現'$ firebaseUtils.toJSON'正在刪除這些屬性。 –

回答

0

這ocurring的原因是因爲AngularFire內建方法$firebaseUtils.toJSON刪除一些前綴屬性。

我解決了我的問題,將.toJSON()添加到我的對象模型中。

MyObject.prototype = { 
    toJSON: function() { 
     return angular.copy(this); 
    } 
};