Merge pull request #3101 from in-async/angularfire.d.ts

update angular/angularfire.d.ts and angularfire-tests.ts
This commit is contained in:
Masahiro Wakame
2014-11-09 00:39:32 +09:00
2 changed files with 207 additions and 53 deletions
+152 -39
View File
@@ -3,53 +3,166 @@
var myapp = angular.module("myapp", ["firebase"]);
interface AngularFireScope extends ng.IScope {
items: AngularFire;
remoteItems: RemoteItems;
}
interface RemoteItems {
bar: string;
data: any;
}
var url = "https://myapp.firebaseio.com";
myapp.controller("MyController", ["$scope", "$firebase",
function($scope: AngularFireScope, $firebase: AngularFireService) {
$scope.items = $firebase(new Firebase(url));
$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, (<any>$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();
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);
// 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 });
// 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);
});
}
// 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 " + (<any>list).sum() + " items");
});
}
$scope.items.$bind($scope, "remoteItems");
$scope.remoteItems.bar = "foo";
$scope.items.$bind($scope, "remote").then(function(unbind) {
unbind();
$scope.remoteItems.bar = "foo";
});
}
]);
var foo: AngularFireObject = {
$priority: 0
};
interface AngularFireAuthScope extends ng.IScope {
loginObj: AngularFireAuth;
}
+55 -14
View File
@@ -1,4 +1,4 @@
// Type definitions for AngularFire 0.6.0
// Type definitions for AngularFire 0.8.2
// Project: http://angularfire.com
// Definitions by: Dénes Harmath <http://github.com/thSoft>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
@@ -7,24 +7,65 @@
/// <reference path="../firebase/firebase.d.ts"/>
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<any>;
$asArray(): AngularFireArray;
$asObject(): AngularFireObject;
$ref(): Firebase;
$push(data: any): ng.IPromise<Firebase>;
$set(key: string, data: any): ng.IPromise<Firebase>;
$set(data: any): ng.IPromise<Firebase>;
$remove(key?: string): ng.IPromise<Firebase>;
$update(key: string, data: Object): ng.IPromise<Firebase>;
$update(data: any): ng.IPromise<Firebase>;
$transaction(updateFn: (currentData: any) => any, applyLocally?: boolean): ng.IPromise<IFirebaseDataSnapshot>;
$transaction(key:string, updateFn: (currentData: any) => any, applyLocally?: boolean): ng.IPromise<IFirebaseDataSnapshot>;
}
interface AngularFireObject {
$priority: number;
interface AngularFireObject extends AngularFireSimpleObject {
$id: string;
$priority: number;
$value: any;
$save(): ng.IPromise<Firebase>;
$loaded(resolve?: (x: AngularFireObject) => ng.IHttpPromise<{}>, reject?: (err: any) => any): ng.IPromise<AngularFireObject>;
$loaded(resolve?: (x: AngularFireObject) => ng.IPromise<{}>, reject?: (err: any) => any): ng.IPromise<AngularFireObject>;
$loaded(resolve?: (x: AngularFireObject) => void, reject?: (err: any) => any): ng.IPromise<AngularFireObject>;
$inst(): AngularFire;
$bindTo(scope: ng.IScope, varName: string): ng.IPromise<any>;
$watch(callback: Function, context?: any): Function;
$destroy(): void;
}
interface AngularFireObjectService {
$extendFactory(ChildClass: Object, methods?: Object): Object;
}
interface AngularFireArray extends Array<AngularFireSimpleObject> {
$add(newData: any): ng.IPromise<Firebase>;
$save(recordOrIndex: any): ng.IPromise<Firebase>;
$remove(recordOrIndex: any): ng.IPromise<Firebase>;
$getRecord(key: string): AngularFireSimpleObject;
$keyAt(recordOrIndex: any): string;
$indexFor(key: string): number;
$loaded(resolve?: (x: AngularFireArray) => ng.IHttpPromise<{}>, reject?: (err: any) => any): ng.IPromise<AngularFireArray>;
$loaded(resolve?: (x: AngularFireArray) => ng.IPromise<{}>, reject?: (err: any) => any): ng.IPromise<AngularFireArray>;
$loaded(resolve?: (x: AngularFireArray) => void, reject?: (err: any) => any): ng.IPromise<AngularFireArray>;
$inst(): AngularFire;
$watch(cb: (event: string, key: string, prevChild: string) => void, context?: any): Function;
$destroy(): void;
}
interface AngularFireArrayService {
$extendFactory(ChildClass: Object, methods?: Object): Object;
}
interface AngularFireSimpleObject {
$id: string;
$priority: number;
$value: any;
[key: string]: any;
}
interface AngularFireAuthService {
(firebase: Firebase): AngularFireAuth;
@@ -34,7 +75,7 @@ interface AngularFireAuth {
$getCurrentUser(): ng.IPromise<any>;
$login(provider: string, options?: Object): ng.IPromise<any>;
$logout(): void;
$createUser(email: string, password: string, noLogin?: boolean): ng.IPromise<any>;
$createUser(email: string, password: string): ng.IPromise<any>;
$changePassword(email: string, oldPassword: string, newPassword: string): ng.IPromise<any>;
$removeUser(email: string, password: string): ng.IPromise<any>;
$sendPasswordResetEmail(email: string): ng.IPromise<any>;