Last active
October 5, 2016 03:15
-
-
Save hutch120/2fde68d8023565f75f08f5eedfa97838 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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