The idea: This post we are going to learn how to build a Firebase Forage with object related database.

Form the pic we can see that there are two object: users and items. This helps to orginzie the data structure better.

The way to do this is following:

We have a base firebase uri

app.constant('FIREBASE_URI', 'https://zhentiw-angular-fire.firebaseio.com/');

Here we create a users object database:

var userRef = new Firebase(FIREBASE_URI + 'users');

Here is how we create a items object database:

var ref = new Firebase(FIREBASE_URI + 'items');

Yeap, it is quite simple and not hard at all. But for the beginner we have comfuses at the begining.

Here is an example how we add data into those two objects.

We have an select tag for display all users.

        <select class="form-control" ng-model="currentUser" ng-options="userId as user.name for (userId, user) in users">
<option value="">Select a user</option>
</select>

Notice the words here:

  userId as user.name for (userId, user) in users, looks like coffeeScript

Then forms for add users and items, and also for displaying items.

Code:

<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
<meta charset="UTF-8">
<title>Angular Firebase</title>
<link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="bower_components/angular-material/angular-material.min.css" rel="stylesheet" />
</head>
<body ng-controller="MainCtrl">
<ui-view>
<button class="btn-info" ui-sref="login()">FirebaseSimpleLogin</button>
<h2>Current User</h2>
<div class="well">
<select class="form-control" ng-model="currentUser" ng-options="userId as user.name for (userId, user) in users">
<option value="">Select a user</option>
</select>
<h3>Add User</h3>
<form class="form-inline" role="form" novalidate ng-submit="addUser()">
<div class="form-group">
<input type="text" class="form-control" ng-model="newUser.name" placeholder="Name">
</div>
<button type="submit" class="btn btn-default">Add</button>
</form>
</div> <h2>Current Item</h2>
<table class="table edit">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Count</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(id, item) in items">
<td><input type="text" ng-model="item.name" ng-blur="updateItem(id)"/></td>
<td><input type="text" ng-model="item.description" ng-blur="updateItem(id)"/></td>
<td><input type="text" ng-model="item.count" ng-blur="updateItem(id)"/></td>
<td>
<a href="#" ng-click="removeItem(id)" class="navbar-link">Remove</a>
</td>
</tr>
</tbody>
</table> <div class="well">
<h4>Add Item</h4> <form class="form-inline" role="form" novalidate ng-submit="addItem()">
<div class="form-group">
<input type="text" class="form-control" ng-model="newItem.name" placeholder="Name">
</div>
<div class="form-group">
<input type="text" class="form-control" ng-model="newItem.description" placeholder="Description">
</div>
<div class="form-group">
<input type="text" class="form-control" ng-model="newItem.count" placeholder="Count">
</div>
<button type="submit" class="btn btn-default">Add</button>
</form>
</div> </ui-view> <script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-material/angular-material.js"></script>
<script src="bower_components/firebase/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/0.6.0/angularfire.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="bower_components/oclazyload/dist/ocLazyLoad.min.js"></script>
<script src="public/js/app.js"></script>
</body>
</html>
/**
* Created by Answer1215 on 11/9/2014.
*/ var app = angular.module('app', ['ui.router','firebase', 'oc.lazyLoad']); app.constant('FIREBASE_URI', 'https://<your app>.firebaseio.com/'); app.config(function($stateProvider){
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'public/login.tmpl.html',
controller: 'LoginCtrl',
resolve: {
'login@': function($ocLazyLoad){
return $ocLazyLoad.load(
{
name: "login", //module name is "store"
files: ["public/js/login.js",
'bower_components/firebase-simple-login/firebase-simple-login.js']
}
)
}
}});
}); app.controller('MainCtrl', ['$scope', 'ItemsService', 'UserService',function ($scope, ItemsService, UserService) {
$scope.newItem = { name: '', description: '', count: 0 };
$scope.newUser = {name: ''};
$scope.currentItem = null;
$scope.isUpdated = false; $scope.items = ItemsService.getItems();
$scope.users = UserService.getUsers(); $scope.items.$on('change', function(){
if(!$scope.isUpdated){return;}
console.log("ITEMS CHANGE");
}); $scope.items.$on('loaded', function(){
console.log("ITEMS LOADED");
}); //Deattach the change event from the items
//$scope.items.$off('change'); $scope.addItem = function () {
ItemsService.addItem(angular.copy($scope.newItem));
$scope.newItem = { name: '', description: '', count: 0 };
}; $scope.updateItem = function (id){
$scope.isUpdated = true;
ItemsService.updateItem(id);
}; $scope.removeItem = function (id) {
ItemsService.removeItem(id);
}; $scope.addUser = function(){
UserService.addUser(angular.copy($scope.newUser));
$scope.newUser = {name: ''};
}
}]); app.factory('UserService', ['$firebase', 'FIREBASE_URI', function ($firebase, FIREBASE_URI) {
var userRef = new Firebase(FIREBASE_URI + 'users');
var users = $firebase(userRef); var getUsers = function () {
return users;
}; var addUser = function (user) {
users.$add(user);
}; return {
getUsers: getUsers,
addUser: addUser
}
}]); app.factory('ItemsService', ['$firebase', 'FIREBASE_URI', function ($firebase, FIREBASE_URI) {
var ref = new Firebase(FIREBASE_URI + 'items');
var items = $firebase(ref); var getItems = function () {
return items;
}; var addItem = function (item) {
items.$add(item);
}; var updateItem = function (id) {
items.$save(id);
}; var removeItem = function (id) {
items.$remove(id);
}; return {
getItems: getItems,
addItem: addItem,
updateItem: updateItem,
removeItem: removeItem
}
}]);

[Firebase] 4. Firebase Object related Database的更多相关文章

  1. [React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD

    Install: npm i --save firebase // v3.2.1 Config Firebase: First we need to require Firebase: import ...

  2. Linked Server: EXECUTE permission denied on object 'xp_prop_oledb_provider', database 'master', owner 'dbo'

    问题出现环境: 使用SQL Server Management Studio 2008 连接到SQL Server 2000的数据库,点击其中一个Oracle链接服务器,单击"目录" ...

  3. 数据库内存泄漏——A SQLiteConnection object for database '/data/data/.../databases/....db' was leaked!

      详细异常: A SQLiteConnection object for database '/data/data/.../database/....db' was leaked!  Please ...

  4. PASCAL VOC数据集The PASCAL Object Recognition Database Collection

    The PASCAL Object Recognition Database Collection News 04-Apr-07: The VOC2007 challenge development ...

  5. A SQLiteConnection object for database '/data/data/.../databases/....db' was leaked!

      详细异常: A SQLiteConnection object for database '/data/data/.../databases/....db' was leaked!  Please ...

  6. profile name is not valid,The EXECUTE permission was denied on the object 'sp_send_dbmail', database 'msdb', schema 'dbo'.

    使用不是sysadmin权限的账号执行存储发邮件,报异常profile name is not valid, EXEC msdb.dbo.sp_send_dbmail @profile_name = ...

  7. JDBC ORM(Object Relationship Database Mapping)

    ORM=Object Relationship Database Mapping 对象和关系数据库的映射 简单说,一个对象,对应数据库里的一条记录 示例:根据id返回一个Hero对象 提供方法get( ...

  8. [Firebase] 2. Firebase Event Handling

    /** * Created by Answer1215 on 11/9/2014. */ var app = angular.module('app', ['firebase']); app.cons ...

  9. [Firebase] 3. Firebase Simple Login Form

    Using $firebaseSimpleLogin service. Here we use three methods for login, logout, register and getCur ...

随机推荐

  1. scrapy抓取拉勾网职位信息(五)——代码优化

    上一篇我们已经让代码跑起来,各个字段也能在控制台输出,但是以item类字典的形式写的代码过于冗长,且有些字段出现的结果不统一,比如发布日期. 而且后续要把数据存到数据库,目前的字段基本都是string ...

  2. redis优化方案

    流水线(pipelined) 批量提交redis命令,减少通信次数 事务 mulit,事务的开始 exec,执行事务快内的命令 discard,放弃事务快内的命令 watch,监视key,如果key改 ...

  3. Java反射机制demo(一)—实例化Class对象,并获得其他类包名和类型

    Java反射机制demo(一)——实例化Class对象,通过对象获得包名和类型 使用Java中的 本地类作为实验对象,避免自定义的类带来的一些不便. public static void main(S ...

  4. Python操作Mongo数据库

    连接数据库 import pymongo # 连接到数据库,以下两种方式均可 client = pymongo.MongoClient(host='localhost', port=27017) cl ...

  5. HDU3530【STL/单调队列/RMQ】

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=3530] 题意:输入n,m,k;n代表n个点,在这n(n<100000)个点中找到最长的连续子序列 ...

  6. BZOJ3619 [Zjoi2014]璀灿光华 构造+dfs

    题意:有一个\(a^3\)个小正方体组成的大正方体,其中有n个正方体会向上下左右前后六个方向中的一个发出光,正方体是透光的,被照亮的正方体有个美丽值\(g_{i}\),给出正方体的相邻关系,问美丽值之 ...

  7. 【洛谷】P1196 [NOI2002]银河英雄传说【带权并查集】

    P1196 [NOI2002]银河英雄传说 题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的 ...

  8. Codeforces Beta Round #7 B. Memory Manager 模拟题

    B. Memory Manager 题目连接: http://www.codeforces.com/contest/7/problem/B Description There is little ti ...

  9. js之iframe父、子页面通信

    注意事项 一 . 页面加载顺序:一般先加载完父页面才会去加载子页面,所以:必须要确保在iframe加载完成后再进行操作,如果iframe还未加载完成就开始调用里面的方法或变量,会产生错误.判断ifra ...

  10. oc(object-c)知识汇总/总结/区别对比(持续更新)

    1.判断某个类是否实现了某方法: A *a =[[A alloc] autorelease]; if([a respondsToSelector:@selector(methodName)]){ // ...