AngularFire扩展了服务问题 [英] AngularFire extending the service issue
问题描述
我一直在寻找Synchronized Arrays的文档 https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-extending-the-services 和 https://www.firebase.com/docs/web/libraries/angular/guide/ extensions-services.html#section-firebasearray
我使用的是Firebase 2.2.7版本和AngularFire 1.1.2版本。 b
使用下面的代码,我很难识别$$已移除的事件。
.factory (ExtendedCourseList,[$ firebaseArray,function($ firebaseArray){
//创建一个基于$ firebaseArray的新服务
ExtendedCourseList = $ firebaseArray。$ extend({
$ $ added:function(dataSnapshot,prevChild){
var course = d ataSnapshot.val();
var course_key = dataSnapshot.key();
console.log(new course);
回报当然;
},
$$ removed:function(snap){
console.log(removed);
返回true;
}
});
返回函数(listRef){
return new ExtendedCourseList(listRef); (CompanyRefObj,function(CompanyRef){
// CompanyRef是一个包含url字符串的常量
var ref = new Firebase(CompanyRef);
return ref;
})
$ b $ .factory('CourseList',function(localstorage,$ rootScope,ExtendedCourseList,CompanyRefObj){
var companyID = localstorage.get(company);
$ rootScope.courseList = ExtendedCourseList(CompanyRefObj.child(companyID).child(courses));
)
如果我运行这个代码,只会触发$$添加的事件。为了模拟移除事件,我使用Firebase中的Web界面来显示数据,在那里我按下删除按钮并接受永久删除的数据。
另外,如果我删除$$去掉的函数,当记录被删除时,扩展服务仍然不会同步。如果我修改我的代码使用$ firebaseArray而不是扩展服务(如上所示),添加和删除事件将被识别。$ b $
.factory('CourseList',function(localstorage,$ rootScope,$ firebaseArray,CompanyRefObj){
var companyID = localstorage.get(company);
$ rootScope.courseList = $ firebaseArray(CompanyRefObj.child(companyID).child(courses));
)
最后,有没有错误的做法会导致某些扩展函数无法工作?
已解决
$$新增:function(dataSnapshot,prevChild) {
var course = dataSnapshot.val();
var course_key = dataSnapshot.key();
//在
课程下修改$ id = course_key;
//修改结束
console.log(new course);
回报当然;
=https://github.com/firebase/angularfire/issues/638 =nofollow> firebase / angularfire github上的问题我收到了一个答案,解决了我的问题。当 $$ added
被提供的代码覆盖时, $ firebaseArray
也失去了内部记录 $ ID
。
添加以下代码行: course。$ id = course_key;
在返回课程之前,使AngularFire识别出该记录已从服务器中删除。
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;
}
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.
这篇关于AngularFire扩展了服务问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!