diff --git a/angularfire/angularfire-tests.ts b/angularfire/angularfire-tests.ts index eced9d7fe..74a6c5a30 100644 --- a/angularfire/angularfire-tests.ts +++ b/angularfire/angularfire-tests.ts @@ -3,7 +3,7 @@ var myapp = angular.module("myapp", ["firebase"]); interface AngularFireScope extends ng.IScope { - items: AngularFire; + items: AngularFireArray; remoteItems: RemoteItems; } @@ -14,23 +14,46 @@ interface RemoteItems { var url = "https://myapp.firebaseio.com"; myapp.controller("MyController", ["$scope", "$firebase", - function($scope: AngularFireScope, $firebase: AngularFireService) { - $scope.items = $firebase(new Firebase(url)); + function ($scope: AngularFireScope, $firebase: AngularFireService) { + var sync = $firebase(new Firebase(url)); + + sync.$asArray() + .$loaded() + .then(function (list: AngularFireArray) { + console.log("list has " + list.length + " items"); + + list.$add({ foo: "bar" }).then(function (ref) { + ref.on("value", function (snapshot) { + if (snapshot.val().foo !== "bar") throw "error"; + }); + }); + + 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" }); + $scope.items.$set({ bar: "baz" }); var keys = $scope.items.$getIndex(); - keys.forEach(function(key, i) { + keys.forEach(function (key, i) { console.log(i, ($scope.items)[key]); }); - $scope.items.$on("loaded", function() { + $scope.items.$on("loaded", function () { console.log("Initial data received!"); }); - $scope.items.$on("change", function() { + $scope.items.$on("change", function () { console.log("A remote change was applied locally!"); }); $scope.items.$off('loaded'); @@ -39,7 +62,7 @@ myapp.controller("MyController", ["$scope", "$firebase", } $scope.items.$bind($scope, "remoteItems"); $scope.remoteItems.bar = "foo"; - $scope.items.$bind($scope, "remote").then(function(unbind) { + $scope.items.$bind($scope, "remote").then(function (unbind) { unbind(); $scope.remoteItems.bar = "foo"; }); @@ -55,7 +78,7 @@ interface AngularFireAuthScope extends ng.IScope { } myapp.controller("MyAuthController", ["$scope", "$firebaseSimpleLogin", - function($scope: AngularFireAuthScope, $firebaseSimpleLogin: AngularFireAuthService) { + function ($scope: AngularFireAuthScope, $firebaseSimpleLogin: AngularFireAuthService) { var dataRef = new Firebase(url); $scope.loginObj = $firebaseSimpleLogin(dataRef); $scope.loginObj.$getCurrentUser().then(_ => { @@ -65,9 +88,9 @@ myapp.controller("MyAuthController", ["$scope", "$firebaseSimpleLogin", $scope.loginObj.$login('password', { email: email, password: password - }).then(function(user) { + }).then(function (user) { console.log('Logged in as: ', user.uid); - }, function(error) { + }, function (error) { console.error('Login failed: ', error); }); $scope.loginObj.$logout(); diff --git a/angularfire/angularfire.d.ts b/angularfire/angularfire.d.ts index 3f2b0aa10..f81ac243f 100644 --- a/angularfire/angularfire.d.ts +++ b/angularfire/angularfire.d.ts @@ -1,4 +1,4 @@ -// Type definitions for AngularFire 0.6.0 +// Type definitions for AngularFire 0.8.2 and Firebase Simple Login 1.6.4 // Project: http://angularfire.com // Definitions by: Dénes Harmath // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -7,25 +7,53 @@ /// interface AngularFireService { - (firebase: Firebase): AngularFire; + (firebase: Firebase, config?:any): AngularFire; } interface AngularFire { - $add(value: any): void; - $remove(key?: string): void; - $save(key?: string): void; - $child(key: string): AngularFire; - $set(value: any): void; - $getIndex(): string[]; - $on(eventType: string, callback: (dataSnapshot: IFirebaseDataSnapshot, prevChildName?: string) => void, cancelCallback?: ()=> void, context?: Object): (dataSnapshot: IFirebaseDataSnapshot, prevChildName?: string) => void; - $off(eventType?: string, callback?: (dataSnapshot: IFirebaseDataSnapshot, prevChildName?: string) => void, cancelCallback?: ()=> void, context?: Object): (dataSnapshot: IFirebaseDataSnapshot, prevChildName?: string) => void; - $bind($scope: ng.IScope, modelName: string): ng.IPromise; + $asArray(): AngularFireArray; + $asObject(): AngularFireObject; + $ref(): Firebase; + $push(data: any): ng.IPromise; + $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(data: any): ng.IPromise; + $transaction(updateFn: (currentData: any) => any, applyLocally?: boolean): ng.IPromise; } interface AngularFireObject { + $id: string; $priority: number; + $value: any; + $save(): ng.IPromise; + $loaded(): ng.IPromise; + $inst(): Firebase; + $bindTo(scope: ng.IScope, varName: string): ng.IPromise; + $watch(callback: Function, context: any): Function; + $destroy(): void; + + $extendFactory(ChildClass: Object, methods?: Object); } +interface AngularFireArray extends Array { + $add(newData: any): ng.IPromise; + $save(recordOrIndex: any): ng.IPromise; + $remove(recordOrIndex: any): ng.IPromise; + $getRecord(key: string): any; + $keyAt(recordOrIndex: any): string; + $indexFor(key: string): number; + $loaded(): ng.IPromise; + $inst(): Firebase; + $watch(cb: (event: string, key: string, prevChild: string) => void, context?: any): Function; + $destroy(): void; + + $extendFactory(ChildClass:Object, methods?:Object); +} + + + interface AngularFireAuthService { (firebase: Firebase): AngularFireAuth; } @@ -34,7 +62,7 @@ interface AngularFireAuth { $getCurrentUser(): ng.IPromise; $login(provider: string, options?: Object): ng.IPromise; $logout(): void; - $createUser(email: string, password: string, noLogin?: boolean): ng.IPromise; + $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;