添加属性以下划线为前缀的对象

Add object with properties prefixed by underscore

我想在我的 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;
    });
})

发生这种情况的原因是 AngularFire 内置方法 $firebaseUtils.toJSON 删除了一些前缀属性。

我解决了将 .toJSON() 添加到我的对象模型的问题。

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