Skip to content

Instantly share code, notes, and snippets.

@MariuszWisniewski
Last active August 29, 2015 14:26
Show Gist options
  • Save MariuszWisniewski/2e66f9162e4807056ef2 to your computer and use it in GitHub Desktop.
Save MariuszWisniewski/2e66f9162e4807056ef2 to your computer and use it in GitHub Desktop.
//this function uses callback object, containing sucess and error functions inside it
function GetOne_Callback_Object(DATA_ID) {
//define callback object
var callback = {};
//add to id success function - will be called on succesful calls
callback.success = function(data) {
console.log('Get one with ID ' + DATA_ID + ' titled: ' + data.title);
};
//add to it error function - will be called if there was an error
callback.error = function(data) {
console.log('Get One Fail! - ' + data);
};
//pass here the callback object, which contains both success and error functions
syncano.Data.getOne(PROJECT_ID, COLLECTION_ID, DATA_ID, callback);
}
//this functions uses passing sucess function as callback, and error notification for error handling
function GetOne_Notification(DATA_ID) {
//will be called once for the next error
syncano.once("syncano:error", function(data) {
console.log('Get One Fail! - ' + data);
})
//pass here function object, will be called on success
syncano.Data.getOne(PROJECT_ID, COLLECTION_ID, DATA_ID, function(data) {
//this function body will be called only on success
console.log('Get one with ID ' + DATA_ID + ' titled: ' + data.title);
});
}
//this function uses passing success function as callback, and has no error handling
function GetOne_Success(DATA_ID) {
//pass here function object, will be called on success
syncano.Data.getOne(PROJECT_ID, COLLECTION_ID, DATA_ID, function(data) {
//this function body will be called only on success
console.log('Sucess only - Get one with ID ' + DATA_ID + ' titled: ' + data.title);
});
}
var authData = {
api_key: "API_KEY",
instance: "YOUR_INSTANCE"
};
var PROJECT_ID = 1234;
var COLLECTION_ID = 1234;
syncano.connect(authData, function(auth) {
console.log("Connected");
GetOne_Callback_Object(1234); //not existing object ID - will use callback.error() if object not found
GetOne_Notification(1234); //not existing object ID - will use once("syncano:error", callback()) notification if object not found
});
//global error handling
//instead of above connection method, use this one:
//register for handling all error notifications
syncano.on("syncano:error", function(data) {
//this function will be called on every Syncano error
console.log('Syncano Error! - ' + data);
})
syncano.connect(authData, function(auth) {
console.log("Connected");
GetOne_Success(1234); //not existing object ID - for error will use notification syncano.on("syncano:error", callback()) - it will be called on every error
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment