Using AngularFire, is it possible to to create relational-style databases? Or access the UniqueIDs?

自古美人都是妖i 提交于 2019-12-09 06:45:58

问题


I saw this post on Firebase's blog explaining the best way to create relational data objects using their platform. I'm struggling to translate these concepts to AngularFire, their integration with the AngularJS platform.

Specifically, I'm trying to display two linked data sets that have a one way pointer reference similar to how they described it in this example from their post:

var commentsRef = new Firebase("https://awesome.firebaseio-demo.com/comments");
var linkCommentsRef = new Firebase("https://awesome.firebaseio-demo.com/links/comments");

linkCommentsRef.on("child_added", function(snap) {
     commentsRef.child(snap.name()).once("value", function() {
         // Render the comment on the link page.
     ));
});

Question: Is it possible with the current AngularFile integration to make the pointer-style references to other data objects? And if so, can you provide an example?

Edit: I feel like I'd be able to solve these problems if I can just access the unique IDs generated by AngularFire for my data [see below]. How do I access them?


回答1:


Great question!

You do need access to the unique IDs, and we recently added a feature that will give you access to it via angularFireCollection: https://github.com/firebase/angularFire/pull/26.

If you're using the implicit sync method (angularFire), then you already have access to the keys as long as you specify the 4th argument that will set the type of the collection to 'Object':

function MyController($scope, angularFire) {
  var url = 'https://awesome.firebaseio-demo.com/comments';
  var promise = angularFire(url, $scope, 'comments', {});
  promise.then(function() {
    var id = '-lw2NDTiZMFvzEWmSnYn';
    console.log($scope.comments[id]);
  });
}

Hope this helps!




回答2:


Yes you can access the ID! Use the code below!

var chru;
var ref = new Firebase("https://myapp.firebaseio.com/users/");
var sync = $firebase(ref);
var users= syncSelect.$asArray();
$scope.users= users;
users.$loaded().then(function() {
    chru = users.length;
});

$scope.specificIdOnSelect = function() {
    var jj;
    for (jj = 0; jj < chru; jj++) {
        var keyer = users.$keyAt(jj);
        var rec = users.$getRecord(keyer);

        if ($scope.userSelected== rec.name) {
            alert("Unique Key of " + $scope.users+ " is " + users.$keyAt(jj));
        }
    }
};

$scope.allIds = function() {
    var jj;
    for (jj = 0; jj < chru; jj++) {
        var keyer = users.$keyAt(jj);
        var rec = users.$getRecord(keyer);
        alert("Unique Key of " + $scope.users+ " is " + users.$keyAt(jj));
    }
};


来源:https://stackoverflow.com/questions/16879484/using-angularfire-is-it-possible-to-to-create-relational-style-databases-or-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!