Skip to content

Instantly share code, notes, and snippets.

@hutch120
Last active October 5, 2016 03:15
Show Gist options
  • Save hutch120/2fde68d8023565f75f08f5eedfa97838 to your computer and use it in GitHub Desktop.
Save hutch120/2fde68d8023565f75f08f5eedfa97838 to your computer and use it in GitHub Desktop.
'use strict'
/**
* Test Adapter
*/
// A (Adapter)-> TestAdapter function is implemented in order to follow the standard Adapter pattern
function TestAdapter(optionalGlobals) {
/**
* Handle some request
*/
var get = function (args) {
// Creating a promise
var promise = new Promise(function (resolve, reject) {
doSomething(args, resolve, reject)
});
// Return the promise
return promise
}
};
var doSomething = function (args, resolve, reject) {
$.ajax({
url: args.url,
method: "GET",
success: function (data) {
data.args = args // Note: Promise.recolve only accepts one parameter. http://bluebirdjs.com/docs/api/promise.resolve.html
resolve(data)
},
error: function (err) {
err.args = args // Note: Promise.recolve only accepts one parameter. http://bluebirdjs.com/docs/api/promise.resolve.html
reject(err)
}
})
// Adapter pattern (clearly must be at end of adapter)
return {
'get': function (args) {
return get(args)
}
}
}
// End A (Adapter)
function Test() {
/**
* This object
* @type {object}
*/
var _ = this
/**
* Run Test
*/
_.run = function (args, cbSuccess, cbFailure) {
// Executes the method call
TestAdapter()
.get(args)
.then(cbSuccess)
.catch(cbFailure)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment