diff --git a/README.md b/README.md index 395547a6b7..ec2bec078f 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ List of Definitions * [File API: Directories and System](http://www.w3.org/TR/file-system-api/) (by [Kon](http://phyzkit.net/)) * [File API: Writer](http://www.w3.org/TR/file-writer-api/) (by [Kon](http://phyzkit.net/)) * [Finite State Machine](https://github.com/jakesgordon/javascript-state-machine) (by [Boris Yankov](https://github.com/borisyankov)) +* [Firebase](https://www.firebase.com/docs/javascript/firebase) (by [Vincent Bortone](https://github.com/vbortone)) * [FlexSlider](http://www.woothemes.com/flexslider/) (by [Diullei Gomes](https://github.com/Diullei)) * [Foundation](http://foundation.zurb.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [Gamepad](http://www.w3.org/TR/gamepad/) (by [Kon](http://phyzkit.net/)) diff --git a/firebase/firebase-tests.ts b/firebase/firebase-tests.ts new file mode 100644 index 0000000000..dfde8d5607 --- /dev/null +++ b/firebase/firebase-tests.ts @@ -0,0 +1,74 @@ +/// +var AUTH_TOKEN: string = "12345"; +var dataRef:Firebase = new Firebase("https://SampleChat.firebaseio-demo.com/"); +//Log me in +dataRef.auth(AUTH_TOKEN, function(error, result) { + if(error) { + console.log("Login Failed!", error); + } else { + console.log('Authenticated successfully with payload:', result.auth); + console.log('Auth expires at:', new Date(result.expires * 1000)); + } +}); + +//Time to log out! +dataRef.unauth(); + +var usersRef:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/'); +var fredRef:Firebase = usersRef.child('fred'); +var fredFirstNameRef:Firebase = fredRef.child('name/first'); +var x:string = fredFirstNameRef.toString(); +// x is now 'https://SampleChat.firebaseIO-demo.com/users/fred/name/first'. + +var usersRef2:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/'); +var sampleChatRef:Firebase = usersRef2.parent(); +var x2:string = sampleChatRef.toString(); +// x is now 'https://SampleChat.firebaseIO-demo.com'. +var y:Firebase = sampleChatRef.parent(); +// y is now null, since sampleChatRef refers to the root of the Firebase. + +var fredRef2:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred'); +var sampleChatRef2 :Firebase= fredRef2.root(); +var x3:string = sampleChatRef2.toString(); +// x is now 'https://SampleChat.firebaseIO-demo.com'. + +var fredRef3:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred'); +var x4:string = fredRef3.name(); +// x is now 'fred'. + +// Increment Fred's rank by 1. +var fredRankRef:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred/rank'); +fredRankRef.transaction(function(currentRank: number) { + return currentRank+1; +}); + +// Try to create a user for wilma, but only if the user id 'wilma' isn't already taken. +var wilmaRef: Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/wilma'); +wilmaRef.transaction(function(currentData) { + if (currentData === null) { + return {name: {first: 'Wilma', last: 'Flintstone'} }; + } else { + console.log('User wilma already exists.'); + return; // Abort the transaction. + } +}, function(error: any, committed: bool, snapshot: IFirebaseDataSnapshot) { + if (error) + console.log('Transaction failed abnormally!', error); + else if (!committed) + console.log('We aborted the transaction (because wilma already exists).'); + else + console.log('User wilma added!'); + console.log('Wilma\'s data: ', snapshot.val()); +}); + +var messageListRef: Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/message_list'); +var lastMessagesQuery:IFirebaseQuery = messageListRef.endAt().limit(500); +lastMessagesQuery.on('child_added', function(childSnapshot: IFirebaseDataSnapshot) { /* handle child add */ }); + +var messageListRef2:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/message_list'); +var firstMessagesQuery:IFirebaseQuery = messageListRef2.startAt().limit(500); +firstMessagesQuery.on('child_added', function(childSnapshot: IFirebaseDataSnapshot) { /* handle child add */ }); + +var usersRef3: Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users'); +var usersQuery: IFirebaseQuery = usersRef3.startAt(1000).limit(50); +usersQuery.on('child_added', function(userSnapshot: IFirebaseDataSnapshot) { /* handle user */ }); diff --git a/firebase/firebase.d.ts b/firebase/firebase.d.ts new file mode 100644 index 0000000000..f50919134b --- /dev/null +++ b/firebase/firebase.d.ts @@ -0,0 +1,74 @@ +// Type definitions for Firebase API +// Project: https://www.firebase.com/docs/javascript/firebase/index.html +// Definitions by: Vincent Botone +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface IFirebaseAuthResult { + auth: any; + expires: number; +} + +interface IFirebaseDataSnapshot { + val(): any; + child(): IFirebaseDataSnapshot; + forEach(childAction: (childSnapshot: IFirebaseDataSnapshot) => bool): bool; + hasChild(childPath: string): bool; + hasChildren(): bool; + name(): string; + numChildren(): number; + ref(): Firebase; + getPriority(): string; + getPriority(): number; + exportVal(): Object; +} + +interface IFirebaseOnDisconnect { + set(value: any, onComplete?: (error: any) => void): void; + setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void; + setWithPriority(value: any, priority: number, onComplete?: (error: any) => void): void; + update(value: any, onComplete?: (error: any) => void): void; + remove(onComplete?: (error: any) => void): void; + cancel(onComplete?: (error: any) => void): void; +} + +interface IFirebaseQuery { + 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, context?: Object): void; + once(eventType: string, successCallback: (dataSnapshot: IFirebaseDataSnapshot) => void, failureCallback?: () => void, context?: Object): void; + limit(limit: number): IFirebaseQuery; + startAt(priority?: string, name?: string): IFirebaseQuery; + startAt(priority?: number, name?: string): IFirebaseQuery; + endAt(priority?: string, name?: string): IFirebaseQuery; + endAt(priority?: number, name?: string): IFirebaseQuery; + ref(): Firebase; +} + +class Firebase implements IFirebaseQuery { + constructor(firebaseURL: string); + auth(authToken: string, onComplete?: (error: string, result: IFirebaseAuthResult) => void, onCancel?:(error: string) => void): void; + unauth(): void; + child(childPath: string): Firebase; + parent(): Firebase; + root(): Firebase; + name(): string; + toString(): string; + set(value: any, onComplete?: (error: any) => void): void; + update(value: any, onComplete?: (error: any) => void): void; + remove(onComplete?: (error: any) => void); + push(value: any, onComplete?: (error: any) => void): Firebase; + setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void; + setWithPriority(value: any, priority: number, onComplete?: (error: any) => void): void; + setPriority(priority: string, onComplete?: (error: any) => void): void; + setPriority(priority: number, onComplete?: (error: any) => void): void; + transaction(updateFunction: (currentData: any)=> any, onComplete?: (error: any, committed: bool, snapshot: IFirebaseDataSnapshot) => void, applyLocally?: bool): void; + onDisconnect(): IFirebaseOnDisconnect; + 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, context?: Object): void; + once(eventType: string, successCallback: (dataSnapshot: IFirebaseDataSnapshot) => void, failureCallback?: () => void, context?: Object): void; + limit(limit: number): IFirebaseQuery; + startAt(priority?: string, name?: string): IFirebaseQuery; + startAt(priority?: number, name?: string): IFirebaseQuery; + endAt(priority?: string, name?: string): IFirebaseQuery; + endAt(priority?: number, name?: string): IFirebaseQuery; + ref(): Firebase; +} \ No newline at end of file