【问题标题】:Indexing firebase objects索引 Firebase 对象
【发布时间】:2014-02-02 10:15:00
【问题描述】:

如何为要传递到 Firebase 的对象数据创建索引?

我正在使用 AngularFire 库中的 .$add 函数来推送数据。这是我正在使用的过滤器和控制器:

angular.module('bestDay', ["firebase"]).factory("GreatService", ["$firebase", function($firebase) {
    var ref = new Firebase("https://quickjournal.firebaseIO.com/");
    return $firebase(ref);
}])
.controller("bdctrl", ["$scope", "GreatService",
    function($scope, greatService) {
        $scope.theval = "Val " + Math.round(Math.random()*101);
        $scope.daylist = greatService;
        $scope.addDayGood = function() {
            $scope.daylist.$add({
                desc: $scope.newDay.desc,
                date: $scope.newDay.date,
                value: $scope.theval
            });
            $scope.newDay.desc = "";
            $scope.newDay.date = "";
        };
    }
]);

如您所见,我在传入对象时尝试使用唯一值,但每次只生成相同的数字 (13)。如果不是很明显,我对编程是半新的。

我还希望能够编写一个函数来删除该索引的数据。由于我无法完成先前的任务,因此我可能也需要帮助来完成此任务。 我正在用 angularjs 库编写我的代码。

我已经梳理了 firebase 和 angularfire 库文档,但没有任何结果。如果您可以将我指向一个包含此文档的 URL,将不胜感激。

【问题讨论】:

标签: javascript angularjs firebase angularfire


【解决方案1】:

Firebase 应该进行索引,因为如果您有多个用户访问相同的数据,这会更容易。

与您的问题相关,您应该查找 https://www.firebase.com/docs/ordered-data.html 以使用 firebase 中的列表。

更重要的是,提供的push() 函数可以轻松按时间排序,如果您需要更复杂的排序,可以查看setWithPriority() 函数。

angular.module('bestDay', ["firebase"])
.controller("bdctrl", ['$scope', '$firebase',
    function($scope,$firebase) {
        var daysRef = new Firebase("https://quickjournal.firebaseIO.com/daylist/");
        $scope.dayList = $firebase(daysRef);
        $scope.dayLocationInFirebase = daysRef.push();
        $scope.addDayGood = function(){
                              // Setdata to the generated location
                              $scope.dayLocationInFirebase.set({
                                                 desc: $scope.newDay.desc,
                                                 date: $scope.newDay.date
                                             });

                              //holds reference to location the object was pushed to, for direct manipulation of the value. Pass it to the scope or an array if you need it for later
                              var pushedName = $scope.dayLocationInFirebase.name();
                               alert(pushedName);
                              $scope.newDay.desc = "";
                              $scope.newDay.date = "";
                          }


    }
]);

【讨论】:

  • 此代码无效。我的 .push() 方法出现错误: TypeError: Object # has no method 'push' 这是因为我的库是如何排序的吗?在 标签中,我传入 angularjs,然后是 firebase.js,然后是 angularfire,然后是我的 javascript script.js 文件。如果您想查看完整代码,这里是 repo:Repo
  • 我从你的 git repo 中编辑了代码,这个工作。注意帽子。 push() 函数采用 firebase 引用。在前面的(和错误的)示例中,我对返回的对象执行了此操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
相关资源
最近更新 更多