AngularFire有时不会更新Firebase

时间:2013-08-08 06:33:58

标签: angularjs-scope firebase angularfire

下面的控制器使用表格和表格提供视图。 该表的相关部分是:

<tr ng-repeat="obj in tab.col | filter:filterObj" ng-click="select(obj)" ng-class="isSelected(obj)">

如果单击一行,表单将显示obj的详细信息(其模型为tab.obj),您可以对其进行编辑。当用户修改表单字段中的obj时,表会适当地获取更新,但firebase不会获得任何更新,而是添加新对象(这是通过array.push(obj)完成的。)

define(['app'], function(app) {
app.controller('LinksCtrl',['$scope', '$filter', '$http','angularFire','angularFireAuth', function($scope,$filter,$http, angularFire, angularFireAuth) {
$scope.links = {};
$scope.tabs = [];

var url = "https://<url>.firebaseio.com/links";
angularFireAuth.initialize(url, {scope: $scope, name: "user", path: "/"});
var promise = angularFire(url, $scope, 'links', {})

$scope.select = function (item) {
  for(i in $scope.tabs) {
    var tab = $scope.tabs[i];
    if (tab.active) {
      tab.obj = item;
      if (tab.obj.selected) {
        tab.obj.selected = ! tab.obj.selected;
      } else {
        tab.obj.selected = true;
        // $scope.$apply();
      }
    }
  }
};

$scope.isSelected = function(obj) {
  if (obj.selected && obj.selected === true) {
   return "active"; 
  }
  return "";
}


promise.then(function() {

  $scope.tabs.push({
    title: 'links',
    disabled: false,
    active: false,
    col: $scope.links.open, //this is an array of objects
    obj: {}
  });

  $scope.tabs[0].active = true;

  for(i in $scope.tabs) {
    var tab = $scope.tabs[i];
    tab.actions = app.actions();

    // app.actions returns an array with more objects like the following doing CRUD and other basic stuff
    tab.actions.push({
      name: 'Delete link',
      icon: 'icon-minus',
      cond: function(tab) { if ('selected' in tab.obj) { return tab.obj.selected}; return false; },
      func: function(tab) {
        // splice tab.col or whatever modification
      }
    });
  };
});
  }]);

});

我怎样才能做出类似这样的工作?应该通过集合转向手动同步?使用console.log进行debuging显示对象obj具有代码的所有阶段(它具有原型,哈希码等)应该具有的内容,只是firebase没有得到更新。

更新

我有一个有效的解决方案。似乎这是一个绑定问题,或类似的东西,但我不知道发生了什么。 tabs [i] .col = $ scope.links.i的赋值看起来就像罪魁祸首(在这种情况下i ='open': - ?。这可能是我对angular的使用。

controller scope ------

    // get the firebase data and store it in 
    var promise = angularFire(url, $scope, 'links', {})

    // build the tabs structure, where tabs[i].col = $scope.links.i

    // build actions for each tab

    // create an empty tabs[i].obj to use in the form model

ng-repeat scope -------

    // iterate over the tab structure
    <tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active" disabled="tab.disabled">

ng-repeat scope --------

    // Iterate over the elements, so we can select one, and edit it, updating firebase
    // If we iterate over tab.col, firebase does not get the updates, but If we iterate over
    // the original $scope.links variable, it works as expected

    <tr ng-repeat="obj in links[tab.title] | filter:filterObj | orderBy: 'date':true" ng-click="select(obj)" ng-class="isSelected(obj)">

1 个答案:

答案 0 :(得分:1)

我有一个有效的解决方案。似乎这是我身边的一个具有约束力的问题。 tabs [i] .col = $ scope.links.i的赋值看起来就像罪魁祸首(在这种情况下,i =&#39; open&#39;: - ?。这可能是我对angular的使用。

解决方案不是使用tabs [i] .col,而是使用$ scope.links,即使用$ scope变量而不是对$ scope变量的引用。

controller scope ------

    // get the firebase data and store it in 
    var promise = angularFire(url, $scope, 'links', {})

    // build the tabs structure, where tabs[i].col = $scope.links.i

    // build actions for each tab

    // create an empty tabs[i].obj to use in the form model

ng-repeat scope -------

    // iterate over the tab structure
    <tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active" disabled="tab.disabled">

ng-repeat scope --------

    // Iterate over the elements, so we can select one, and edit it, updating firebase
    // If we iterate over tab.col, firebase does not get the updates, but If we iterate over
    // the original $scope.links variable, it works as expected

    <tr ng-repeat="obj in links[tab.title] | filter:filterObj | orderBy: 'date':true" ng-click="select(obj)" ng-class="isSelected(obj)">