AngularFire:如何更新非规范化数组/对象?

时间:2015-09-12 08:41:14

标签: angularfire denormalized

我目前正在使用Kato的要点(https://gist.github.com/katowulf/f78d4a224c06a643ddfa)来使用其他Firebase路径规范化我的记录。

我面临的问题是当我尝试将对象更新回firebase数据库时:数据库不会更新(控制台中没有错误消息)。

我的书桌看起来像这样:

books:
    -JyDpkQrUozE3L7flpo: {
    title: "title1",
    author: {-JyDpkQrUozE3L7fl1x: true}
    },
    -JyDpkQrUozE3L7flp1: {
    title: "title2",
    author: {-JyDptrrrezE3L7flKx: true}
    },
    -JyDpkQrUozE3L7flp2: {
    title: "title3",
    author: {-JyDpkQrUozE3L7f222: true}
    }

我的作者表格如下:

authors:
    -JyDpkQrUozE3L7flKx: {
        firstname: "jacques",
        lastname: "smith"
    },
    -JyDptrrrezE3L7flKx: {
        firstname: "bill",
        lastname: "halley"
    },
    -JyDpkQrUozE3L7f222: {
        firstname: "john",
        lastname: "ford"
    }

我对normalizedBook的服务如下:

.factory('NormalizedBook’, ['$firebaseObject', 'firebaseService', function($firebaseObject, firebaseService) {
    var service = $firebaseObject.$extend({
        $$updated: function(snap) {
            var self = this;
            var record = $firebaseObject.prototype.$$updated.call(this, snap);
            firebaseService.$getAuthor( self.authorId ).$loaded().then(function( authorData ) {
                    self.authorFirstName = authorData.authorFirstName;
                    self.authorLastName = authorData.authorLastName;
            });
            return record;
        }
    });
    return service;
}])

firebaseService只是一个帮助我检索数据的服务:

var service = {};
service.refAuthors = new Firebase(FBAuthors);
service.authors = $firebaseArray( refAuthors );
service.$getAuthor = function(id) {
    if( !service.authors.hasOwnProperty(id) ) {
        service.authors[id] = $firebaseObject(service.refAuthors.child(id));
    };
    return service.authors[id];
};
…….
return service;

我的控制器看起来像这样:

.controller('booksController', ['$scope', '$firebaseArray', 'normalizedBook', function($scope, $firebaseArray, normalizedBook) {
    $scope.bookSelected = new normalizedBook(firebaseService.refBooks.child(id);    
    $scope.updateBookSelected = function(item) {
        $scope.bookSelected.$save(item);
    };
});

我的视图呈现预期的对象(同一对象中的书和作者信息),所以一切正常。唯一的问题是如何将我的对象更新回firebase数据库。关于如何做到这一点的任何想法?

0 个答案:

没有答案