gpt4 book ai didi

javascript - $firebaseArray 在保存子元素后丢失功能

转载 作者:行者123 更新时间:2023-11-28 07:04:43 25 4
gpt4 key购买 nike

我正在尝试实现列表详细信息 View 。该列表是使用 $firebaseArray(ref) 生成的。当单击列表中的某个项目时,列表 Controller 使用 list.$getRecord(item.$id) 来获取特定记录。将其放入全局服务中(只是带有 set 和 get 的空对象),并在我的详细信息 Controller 中将该服务(已设置为所选项目)分配给详细信息 Controller 中的范围变量并显示它。

详细 View 中的信息是可编辑的。编辑时,会出现一个保存按钮,单击该按钮将使用此代码保存编辑

item = editItem; //editItem contains the new changes made on the detail page 
list.$save(item).then(function(ref){
//ref.key() === item.$id; //
console.log("Your Edit has been saved');
});

这有效。编辑内容反射(reflect)在远程 Firebase 数据上。

但是当我导航回 ListView 并尝试单击另一个项目时,就会出现问题。它收到一个错误,指出 list.$getRecord() 不是函数。现在,当您不在详细信息 View 上保存编辑时,就不会出现此错误。

我在保存之前和之后打印了列表数组,我意识到了这一点

保存项目之前的列表数组(包含 AngularFire 方法) List array before an item is saved

保存项目后的列表数组(不再包含 AngularFire 方法) List array after an item is saved

我不知道为什么 $firebaseArray 会这样 react 。我缺少什么吗?这是正常行为吗?PS:我正在使用 ionic 和 angularFire。

如果需要,我可以发布更多代码

编辑这是代码的抽象

列表.html

<ion-list>
<ion-item href="#/lead/info" ng-click="vm.selectItem(l.$id)" ng-repeat="l in vm.list" >
<h3>{{l.firstName}} {{l.lastName}}</h3>
<h4 class="">
<p>{{l.topic}}</p>
</h4>
</ion-item>
</ion-list>

list.js( Controller 函数)

function ListCtrl(selectedItem, $firebaseArray) {
/* jshint validthis: true */
var vm = this;

vm.list= {};
vm.selectItem = selectItem;
loadList(); //Loads the List array

function loadList() {

var fireList = new Firebase("https://xxxxx.firebaseio.com/list");
var r = $firebaseArray(fireList);

r.$loaded(function () {
vm.list = r;
});

console.log(vm.list); //Outputs the first image(above). But after an item edit and i go back, outputs the second image(above)
}

function selectItem(index) {
var sItem = vm.list.$getRecord(index);

selectedItem.setList(vm.list);
selectedItem.setItem(sItem);
}
}

selectedItem 服务很简单。我用它来设置单个对象或对象数组

function selectedItem() {
var sItem = {};
var List = {};

return {
getItem: function () {
return sItem;
},
setItem: function (authObject) {
sItem = authObject;
},
getList: function(){
return List;
},
setList: function(al){
List = al;
}
};
};

详细 View Controller 是这样的:

item.js( Controller 函数)

function ItemCtrl(selectedItem, $scope, $firebaseObject) {
/* jshint validthis: true */
var vm = this;
$scope.selectedItem = selectedItem.getItem();
$scope.listArray = selectedItem.getList();
//vm.item = $scope.selectedItem;

function saveEdit() {

var t = $scope.selectedItem;

$scope.listArray.$save(t).then(function(ref){
//console.log(ref);
});
}
};

更新

经过认真交叉检查我的代码,我意识到问题不是来自 AngularFiire 数组。即使我使用 r.$watchr.$loaded 所做的解决方法也是不必要的。需要解决这个问题是由我认为不相关的代码的另一部分引起的。我为这个错误道歉。我很快就会删除这个问题和相关问题

最佳答案

尝试使用观察者重新加载数据:

var fireList = new Firebase("https://xxxxx.firebaseio.com/list");
var r = $firebaseArray(fireList);

r.$watch(function() {
r.$loaded(function () {
vm.list = r;
});
});

这是一种简单处理更新的常见方法,可能会解决您的问题。

关于javascript - $firebaseArray 在保存子元素后丢失功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31792734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com