访问firebase中的项目

时间:2014-03-13 21:29:41

标签: angularjs firebase ionic-framework

我正在尝试通过扩展应用程序来使用firebase作为后端来学习firebase / angularjs。

我的伪造看起来像这样

forge view

在我的计划中,我已将firebaseio.com/projects绑定到$scope.projects

我如何接触孩子? 为什么$scope.projects.getIndex()不将密钥返回给孩子?

我知道这些项目位于$scope.projects,因为如果我console.log($scope.projects)我可以看到它们

app.js

angular.module('todo', ['ionic', 'firebase'])
/**
 * The Projects factory handles saving and loading projects
 * from localStorage, and also lets us save and load the
 * last active project index.
 */
.factory('Projects', function() {
  return {
    all: function () {
      var projectString = window.localStorage['projects'];
      if(projectString) {
        return angular.fromJson(projectString);
      }
      return [];
    },
    // just saves all the projects everytime
    save: function(projects) {
      window.localStorage['projects'] = angular.toJson(projects);
    },
    newProject: function(projectTitle) {
      // Add a new project
      return {
        title: projectTitle,
        tasks: []
      };
    },
    getLastActiveIndex: function () {
      return parseInt(window.localStorage['lastActiveProject']) || 0;
    },
    setLastActiveIndex: function (index) {
      window.localStorage['lastActiveProject'] = index;
    }
  }
})

.controller('TodoCtrl', function($scope, $timeout, $ionicModal, Projects, $firebase) {

  // Load or initialize projects
  //$scope.projects = Projects.all();
  var projectsUrl = "https://ionic-guide-harry.firebaseio.com/projects";
  var projectRef = new Firebase(projectsUrl);
  $scope.projects = $firebase(projectRef);
  $scope.projects.$on("loaded", function() {
    var keys = $scope.projects.$getIndex();
    console.log($scope.projects.$child('-JGTmBu4aeToOSGmgCo1'));

    // Grab the last active, or the first project
    $scope.activeProject = $scope.projects.$child("" + keys[0]);
  });

  // A utility function for creating a new project
  // with the given projectTitle
  var createProject = function(projectTitle) {
    var newProject = Projects.newProject(projectTitle);
    $scope.projects.$add(newProject);
    Projects.save($scope.projects);
    $scope.selectProject(newProject, $scope.projects.length-1);
  };



  // Called to create a new project
  $scope.newProject = function() {
    var projectTitle = prompt('Project name');
    if(projectTitle) {
      createProject(projectTitle);
    }
  };

  // Called to select the given project
  $scope.selectProject = function(project, index) {
    $scope.activeProject = project;
    Projects.setLastActiveIndex(index);
    $scope.sideMenuController.close();
  };

  // Create our modal
  $ionicModal.fromTemplateUrl('new-task.html', function(modal) {
    $scope.taskModal = modal;
  }, {
    scope: $scope
  });

  $scope.createTask = function(task) {
    if(!$scope.activeProject || !task) {
      return;
    }
    console.log($scope.activeProject.task);
    $scope.activeProject.task.$add({
      title: task.title
    });
    $scope.taskModal.hide();

    // Inefficient, but save all the projects
    Projects.save($scope.projects);

    task.title = "";
  };

  $scope.newTask = function() {
    $scope.taskModal.show();
  };

  $scope.closeNewTask = function() {
    $scope.taskModal.hide();
  };

  $scope.toggleProjects = function() {
    $scope.sideMenuController.toggleLeft();
  };


  // Try to create the first project, make sure to defer
  // this by using $timeout so everything is initialized
  // properly
  $timeout(function() {
    if($scope.projects.length == 0) {
      while(true) {
        var projectTitle = prompt('Your first project title:');
        if(projectTitle) {
          createProject(projectTitle);
          break;
        }
      }
    }
  });

});

我对底部的物品感兴趣

的console.log($ scope.projects) projects object

更新 在挖掘之后,似乎我可能错误地访问了数据。 https://www.firebase.com/docs/reading-data.html

这是我的新方法

  // Load or initialize projects
  //$scope.projects = Projects.all();
  var projectsUrl = "https://ionic-guide-harry.firebaseio.com/projects";
  var projectRef = new Firebase(projectsUrl);

  projectRef.on('value', function(snapshot) {
    if(snapshot.val() === null) {
      console.log('location does not exist');
    } else {
      console.log(snapshot.val()['-JGTdgGAfq7dqBpSk2ls']);
    }
  });

  $scope.projects = $firebase(projectRef);
  $scope.projects.$on("loaded", function() {
    // Grab the last active, or the first project
    $scope.activeProject = $scope.projects.$child("a");
  });

我仍然不确定如何以编程方式遍历键,但我觉得我已经接近了

2 个答案:

答案 0 :(得分:0)

这是一个包含更多对象的对象,使用for in

进行循环
for (var key in $scope.projects) {
    if ($scope.projects.hasOwnProperty(key)) {
        console.log("The key is: " + key);
        console.log("The value is: " + $scope.projects[key]);
    }
}

答案 1 :(得分:0)

确定val()返回一个对象。为了遍历projects我所做的所有孩子

  // Load or initialize projects
  //$scope.projects = Projects.all();
  var projectsUrl = "https://ionic-guide-harry.firebaseio.com/projects";
  var projectRef = new Firebase(projectsUrl);

  projectRef.on('value', function(snapshot) {
    if(snapshot.val() === null) {
      console.log('location does not exist');
    } else {
      var keys = Object.keys(snapshot.val());
      console.log(snapshot.val()[keys[0]]);
    }
  });

  $scope.projects = $firebase(projectRef);
  $scope.projects.$on("loaded", function() {
    // Grab the last active, or the first project
    $scope.activeProject = $scope.projects.$child("a");
  });

请注意var keys = Object.keys()获取firebaseio.com/projects处的所有密钥,然后您可以通过snapshot.val()[keys[0])

获得第一个孩子