handle promises returned by firebase API

This commit is contained in:
Szymon Stasik committed 2016-03-01 10:40:30 +01:00
1 parent c0d876601e
commit 84bed8dca0
2 files changed
+210 -22

No files matched your search

+174
View File
@@ -27,9 +27,43 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithCustomToken(AUTH_TOKEN).then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authAnonymously()
*/
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authAnonymously().then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
// Log me in
@@ -60,6 +94,25 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithPassword({
"email": "bobtony@firebase.com",
"password": "correcthorsebatterystaple"
}).then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authWithOAuthPopup()
*/
@@ -75,6 +128,23 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithOAuthPopup("twitter").then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authWithOAuthRedirect
*/
@@ -90,6 +160,23 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function () {
// We'll never get here, as the page will redirect on success.
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithOAuthRedirect("twitter").then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authWithOAuthToken()
*/
@@ -104,6 +191,24 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
}
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Authenticate with Facebook using an existing OAuth 2.0 access token
dataRef.authWithOAuthToken("facebook", "<ACCESS-TOKEN>").then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
// Authenticate with Twitter using an existing OAuth 1.0a credential set
@@ -226,6 +331,20 @@ var x4:string = fredRef3.name();
// when the data has finished synchronizing
}
() => {
var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name');
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredNameRef.set({ first: 'Fred', last: 'Flintstone' }).then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
/*
* Firebase.update()
*/
@@ -247,6 +366,21 @@ var x4:string = fredRef3.name();
};
fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete);
}
() => {
var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name');
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredNameRef.update({ first: 'Fred', last: 'Flintstone' }).then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
() => {
var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred');
//The following 2 function calls are equivalent
@@ -276,6 +410,19 @@ var x4:string = fredRef3.name();
// a message when the delete has finished synchronizing
}
() => {
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredRef.remove().then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
/*
* Firebase.push()
*/
@@ -313,6 +460,33 @@ var x4:string = fredRef3.name();
// priority of the data so he'll be ordered relative to other users by his rank
}
() => {
var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred');
var user = {
name: {
first: 'Fred',
last: 'Flintstone'
},
rank: 1000
};
fredRef.setWithPriority(user, 1000);
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredRef.setWithPriority(user, 1000).then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
/*
* Firebase.setPriority()
*/