From 5eef2e22f9980b06625e816bfc47b3bdc37d0f52 Mon Sep 17 00:00:00 2001 From: in-async Date: Thu, 6 Nov 2014 21:27:22 +0900 Subject: [PATCH] update angularfire.d.ts from 0.6.0 to 0.8.2 --- angularfire/angularfire-tests.ts | 208 ++++++++++++++++++++++--------- angularfire/angularfire.d.ts | 59 +++++---- 2 files changed, 185 insertions(+), 82 deletions(-) diff --git a/angularfire/angularfire-tests.ts b/angularfire/angularfire-tests.ts index 74a6c5a302..231c0f992c 100644 --- a/angularfire/angularfire-tests.ts +++ b/angularfire/angularfire-tests.ts @@ -3,76 +3,166 @@ var myapp = angular.module("myapp", ["firebase"]); interface AngularFireScope extends ng.IScope { - items: AngularFireArray; - remoteItems: RemoteItems; -} - -interface RemoteItems { - bar: string; + data: any; } var url = "https://myapp.firebaseio.com"; -myapp.controller("MyController", ["$scope", "$firebase", - function ($scope: AngularFireScope, $firebase: AngularFireService) { - var sync = $firebase(new Firebase(url)); +myapp.controller("MyController", ["$scope", "$firebase", '$FirebaseObject', '$FirebaseArray', + function ($scope: AngularFireScope, $firebase: AngularFireService, $FirebaseObject: AngularFireObjectService, $FirebaseArray: AngularFireArrayService) { + var ref = new Firebase(url); + var sync = $firebase(ref); - sync.$asArray() - .$loaded() - .then(function (list: AngularFireArray) { - console.log("list has " + list.length + " items"); + // AngularFire + { + sync.$asArray(); + sync.$asObject(); + sync.$ref(); + sync.$remove(); + sync.$push({ foo: "foo data" }); + sync.$set("foo", 1); + sync.$set({ foo: 2 }); + sync.$update({ foo: 3 }); + sync.$update("foo", { bar: 1 }); - list.$add({ foo: "bar" }).then(function (ref) { - ref.on("value", function (snapshot) { - if (snapshot.val().foo !== "bar") throw "error"; - }); + // Increment the message count by 1 + sync.$transaction('count', function (currentCount) { + if (!currentCount) return 1; // Initial value for counter. + if (currentCount < 0) return; // Return undefined to abort transaction. + return currentCount + 1; // Increment the count by 1. + }).then(function (snapshot) { + if (!snapshot) { + // Handle aborted transaction. + } else { + // Do something. + console.log(snapshot.val()); + } + }, function (err) { + // Handle the error condition. + console.log(err.stack); }); - - var item = list.$getRecord("foo"); - list.$remove("foo"); - list.$remove(0); - list.$save(); - }); - sync.$asObject() - - - $scope.items = sync.$asArray(); - $scope.object = sync.$asObject(); - - $scope.items.$add({ foo: "bar" }); - $scope.items.$remove("foo"); - $scope.items.$remove(); - $scope.items.$save(); - var child = $scope.items.$child("foo"); - child.$remove(); - $scope.items.$set({ bar: "baz" }); - var keys = $scope.items.$getIndex(); - keys.forEach(function (key, i) { - console.log(i, ($scope.items)[key]); - }); - $scope.items.$on("loaded", function () { - console.log("Initial data received!"); - }); - $scope.items.$on("change", function () { - console.log("A remote change was applied locally!"); - }); - $scope.items.$off('loaded'); - function stopSync() { - $scope.items.$off(); } - $scope.items.$bind($scope, "remoteItems"); - $scope.remoteItems.bar = "foo"; - $scope.items.$bind($scope, "remote").then(function (unbind) { - unbind(); - $scope.remoteItems.bar = "foo"; - }); + + + // AngularFireObject + { + var obj = sync.$asObject(); + + // $id + if (obj.$id !== ref.name()) throw "error"; + + // $loaded() + obj.$loaded().then((data) => { + if (data !== obj) throw "error"; + // $priority + obj.$priority; + + // $value, $save() + obj.$value = "foobar"; + obj.$save(); + }); + + // $inst() + if (obj.$inst() !== sync) throw "error"; + + // $bindTo() + obj.$bindTo($scope, "data").then(function () { + console.log($scope.data); + $scope.data.foo = "baz"; // will be saved to Firebase + sync.$set({ foo: "baz" }); // this would update Firebase and $scope.data + }); + + // $watch() + var unwatch = obj.$watch(function () { + console.log("data changed!"); + }); + unwatch(); + + // $destroy() + obj.$destroy(); + + // $extendFactory() + var NewFactory = $FirebaseObject.$extendFactory({ + getMyFavoriteColor: function () { + return this.favoriteColor + ", no green!"; // obscure Monty Python reference + } + }); + var customObj = $firebase(ref, { objectFactory: NewFactory }).$asObject(); + } + + // AngularFireArray + { + var list = sync.$asArray(); + + // $inst() + if (list.$inst() !== sync) throw "error"; + + // $add() + list.$add({ foo: "foo value" }); + + // $keyAt() + var key = list.$keyAt(0); + + // $indexFor() + var index = list.$indexFor(key); + + // $getRecord() + var item = list.$getRecord(key); + + // $save() + item["bar"] = "bar value"; + list.$save(item); + + // $remove() + list.$remove(item); + + // $loaded() + list.$loaded().then(data => { + if (data !== list) throw "error"; + }); + + // $watch() + var unwatch = list.$watch((event, key, prevChild) => { + switch (event) { + case "child_added": + console.log(key + " added"); + break; + case "child_changed": + console.log(key + " changed"); + break; + case "child_moved": + console.log(key + " moved"); + break; + case "child_removed": + console.log(key + " removed"); + break; + default: + throw "error"; + } + }); + unwatch(); + + // $destroy() + list.$destroy(); + + // $extendFactory() + var ArrayWithSum = $FirebaseArray.$extendFactory({ + sum: function () { + var total = 0; + angular.forEach(this.$list, function (rec) { + total += rec.x; + }); + return total; + } + }); + var list = $firebase(ref, { arrayFactory: ArrayWithSum }).$asArray(); + list.$loaded().then(function () { + console.log("List has " + (list).sum() + " items"); + }); + } } ]); -var foo: AngularFireObject = { - $priority: 0 -}; - interface AngularFireAuthScope extends ng.IScope { loginObj: AngularFireAuth; } diff --git a/angularfire/angularfire.d.ts b/angularfire/angularfire.d.ts index f81ac243fa..b6a0d55d26 100644 --- a/angularfire/angularfire.d.ts +++ b/angularfire/angularfire.d.ts @@ -1,4 +1,4 @@ -// Type definitions for AngularFire 0.8.2 and Firebase Simple Login 1.6.4 +// Type definitions for AngularFire 0.8.2 // Project: http://angularfire.com // Definitions by: Dénes Harmath // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -7,7 +7,7 @@ /// interface AngularFireService { - (firebase: Firebase, config?:any): AngularFire; + (firebase: Firebase, config?: any): AngularFire; } interface AngularFire { @@ -18,52 +18,65 @@ interface AngularFire { $set(key: string, data: any): ng.IPromise; $set(data: any): ng.IPromise; $remove(key?: string): ng.IPromise; - $update(key: string, data: any): ng.IPromise; + $update(key: string, data: Object): ng.IPromise; $update(data: any): ng.IPromise; $transaction(updateFn: (currentData: any) => any, applyLocally?: boolean): ng.IPromise; + $transaction(key:string, updateFn: (currentData: any) => any, applyLocally?: boolean): ng.IPromise; } -interface AngularFireObject { +interface AngularFireObject extends AngularFireSimpleObject { $id: string; $priority: number; $value: any; $save(): ng.IPromise; - $loaded(): ng.IPromise; - $inst(): Firebase; + $loaded(resolve?: (x: AngularFireObject) => ng.IHttpPromise<{}>, reject?: (err: any) => any): ng.IPromise; + $loaded(resolve?: (x: AngularFireObject) => ng.IPromise<{}>, reject?: (err: any) => any): ng.IPromise; + $loaded(resolve?: (x: AngularFireObject) => void, reject?: (err: any) => any): ng.IPromise; + $inst(): AngularFire; $bindTo(scope: ng.IScope, varName: string): ng.IPromise; - $watch(callback: Function, context: any): Function; + $watch(callback: Function, context?: any): Function; $destroy(): void; - - $extendFactory(ChildClass: Object, methods?: Object); +} +interface AngularFireObjectService { + $extendFactory(ChildClass: Object, methods?: Object): Object; } -interface AngularFireArray extends Array { +interface AngularFireArray extends Array { $add(newData: any): ng.IPromise; $save(recordOrIndex: any): ng.IPromise; $remove(recordOrIndex: any): ng.IPromise; - $getRecord(key: string): any; + $getRecord(key: string): AngularFireSimpleObject; $keyAt(recordOrIndex: any): string; $indexFor(key: string): number; - $loaded(): ng.IPromise; - $inst(): Firebase; + $loaded(resolve?: (x: AngularFireArray) => ng.IHttpPromise<{}>, reject?: (err: any) => any): ng.IPromise; + $loaded(resolve?: (x: AngularFireArray) => ng.IPromise<{}>, reject?: (err: any) => any): ng.IPromise; + $loaded(resolve?: (x: AngularFireArray) => void, reject?: (err: any) => any): ng.IPromise; + $inst(): AngularFire; $watch(cb: (event: string, key: string, prevChild: string) => void, context?: any): Function; $destroy(): void; - - $extendFactory(ChildClass:Object, methods?:Object); +} +interface AngularFireArrayService { + $extendFactory(ChildClass: Object, methods?: Object): Object; } +interface AngularFireSimpleObject { + $id: string; + $priority: number; + $value: any; + [key: string]: any; +} interface AngularFireAuthService { - (firebase: Firebase): AngularFireAuth; + (firebase: Firebase): AngularFireAuth; } interface AngularFireAuth { - $getCurrentUser(): ng.IPromise; - $login(provider: string, options?: Object): ng.IPromise; - $logout(): void; - $createUser(email: string, password: string): ng.IPromise; - $changePassword(email: string, oldPassword: string, newPassword: string): ng.IPromise; - $removeUser(email: string, password: string): ng.IPromise; - $sendPasswordResetEmail(email: string): ng.IPromise; + $getCurrentUser(): ng.IPromise; + $login(provider: string, options?: Object): ng.IPromise; + $logout(): void; + $createUser(email: string, password: string): ng.IPromise; + $changePassword(email: string, oldPassword: string, newPassword: string): ng.IPromise; + $removeUser(email: string, password: string): ng.IPromise; + $sendPasswordResetEmail(email: string): ng.IPromise; }