AngularFire extending the service issue

岁酱吖の 提交于 2019-12-25 04:35:19

问题


I've been looking at the documentation for Synchronized Arrays https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-extending-the-services and https://www.firebase.com/docs/web/libraries/angular/guide/extending-services.html#section-firebasearray

I'm using Firebase version 2.2.7 and AngularFire version 1.1.2

Using the code below, I'm having trouble recognizing $$removed events.

.factory("ExtendedCourseList", ["$firebaseArray",  function($firebaseArray) {
  // create a new service based on $firebaseArray
  var ExtendedCourseList= $firebaseArray.$extend({
    $$added: function(dataSnapshot, prevChild){
        var course = dataSnapshot.val();
        var course_key = dataSnapshot.key();
        console.log("new course");
        return course;
    },
    $$removed: function(snap){
        console.log("removed");
        return true;
    }
  });
  return function(listRef) {      
    return new ExtendedCourseList(listRef);
  } 
}]) 

.factory("CompanyRefObj", function(CompanyRef) {
  //CompanyRef is a constant containing the url string  
  var ref = new Firebase(CompanyRef);
  return ref;
})

.factory('CourseList', function (localstorage,$rootScope,ExtendedCourseList,CompanyRefObj) {
    var companyID = localstorage.get("company");   
    $rootScope.courseList = ExtendedCourseList(CompanyRefObj.child(companyID).child("courses")); 
)

If I run this code, only the $$added events will be triggered. To simulate the remove events I use the web-interface at Firebase to display data, where I press the remove button and accept the data being deleted permanently.

Additionally, if I delete the $$removed function, the extended service still won't synchronize when a record is deleted.

If I modify my code to use the $firebaseArray instead of extending the service (as seen above) both add and remove events will be recognized.

 .factory('CourseList', function (localstorage,$rootScope,$firebaseArray,CompanyRefObj) {
    var companyID = localstorage.get("company");   
    $rootScope.courseList = $firebaseArray(CompanyRefObj.child(companyID).child("courses")); 
)

Finally, are there any bad practices I've missed that can cause some of the extended functions to not work?

Solved

$$added: function(dataSnapshot, prevChild){
    var course = dataSnapshot.val();
    var course_key = dataSnapshot.key();
    //Modified below
    course.$id = course_key;
    //End of modification
    console.log("new course");
    return course;
}

回答1:


After posting about the issue at firebase/angularfire github I received an answer that solved my issue. When $$added got overridden by the code provided, the $firebaseArray also lost its internal record $id.

Adding this line of code: course.$id = course_key; before returning the course, made AngularFire recognize when the record was removed from the server.



来源:https://stackoverflow.com/questions/31443066/angularfire-extending-the-service-issue

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