Comparing version
106
main.js
/*! | ||
* ember-ajax | ||
* ic-ajax | ||
* | ||
* - please see license at https://github.com/instructure/ember-ajax | ||
* - inspired by discourse ajax: https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/mixins/ajax.js#L19 | ||
* - please see license at https://github.com/instructure/ember-ajax | ||
*/ | ||
;(function (root, factory) { | ||
;(function(root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define(['ember'], function (Ember) { | ||
return factory(Ember); | ||
}); | ||
define(['ember'], function(Ember) { return factory(Ember); }); | ||
} else if (typeof exports === 'object') { | ||
@@ -18,10 +17,48 @@ module.exports = factory(require('ember')); | ||
} | ||
}(this, function (Ember) { | ||
}(this, function(Ember) { | ||
/* | ||
* jQuery.ajax wrapper, supports the same signature except providing | ||
* `success` and `error` handlers will throw an error (use promises | ||
* instead). | ||
*/ | ||
var ajax = function() { | ||
var settings = parseArgs.apply(null, arguments); | ||
return makePromise(parseArgs.apply(null, arguments)); | ||
}; | ||
/* | ||
* Defines a fixture that will be used instead of an actual ajax | ||
* request to a given url. This is useful for testing, allowing you to | ||
* stub out responses your application will send without requiring | ||
* libraries like sinon or mockjax, etc. | ||
* | ||
* For example: | ||
* | ||
* ajax.defineFixture('/self', { | ||
* response: { firstName: 'Ryan', lastName: 'Florence' }, | ||
* textStatus: 'success' | ||
* jqXHR: {} | ||
* }); | ||
* | ||
* @param {String} url | ||
* @param {Object} fixture | ||
*/ | ||
ajax.defineFixture = function(url, fixture) { | ||
ajax.FIXTURES = ajax.FIXTURES || {}; | ||
ajax.FIXTURES[url] = fixture; | ||
}; | ||
/* | ||
* Looks up a fixture by url. | ||
* | ||
* @param {String} url | ||
*/ | ||
ajax.lookupFixture = function(url) { | ||
return ajax.FIXTURES && ajax.FIXTURES[url]; | ||
}; | ||
function makePromise(settings) { | ||
return new Ember.RSVP.Promise(function(resolve, reject) { | ||
// If we have FIXTURES, load from there instead (testing) | ||
var fixture = ajax.lookupFixture(settings.url); | ||
@@ -31,19 +68,4 @@ if (fixture) { | ||
} | ||
settings.success = function(response, textStatus, jqXHR) { | ||
Ember.run(null, resolve, { | ||
response: response, | ||
textStatus: textStatus, | ||
jqXHR: jqXHR | ||
}); | ||
}; | ||
settings.error = function(jqXHR, textStatus, errorThrown) { | ||
Ember.run(null, reject, { | ||
jqXHR: jqXHR, | ||
textStatus: textStatus, | ||
errorThrown: errorThrown | ||
}); | ||
}; | ||
settings.success = makeSuccess(resolve, reject); | ||
settings.error = makeError(resolve, reject); | ||
Ember.$.ajax(settings); | ||
@@ -53,12 +75,2 @@ }); | ||
ajax.lookupFixture = function(url) { | ||
return ajax.FIXTURES && ajax.FIXTURES[url]; | ||
}; | ||
ajax.defineFixture = function(url, fixture) { | ||
ajax.FIXTURES = ajax.FIXTURES || {}; | ||
ajax.FIXTURES[url] = fixture; | ||
}; | ||
// parses arguments to support `ajax(url, settings)` or `ajax(settings)` | ||
function parseArgs() { | ||
@@ -82,2 +94,22 @@ var settings = {}; | ||
function makeSuccess(resolve, reject) { | ||
return function(response, textStatus, jqXHR) { | ||
Ember.run(null, resolve, { | ||
response: response, | ||
textStatus: textStatus, | ||
jqXHR: jqXHR | ||
}); | ||
} | ||
} | ||
function makeError(resolve, reject) { | ||
return function(jqXHR, textStatus, errorThrown) { | ||
Ember.run(null, reject, { | ||
jqXHR: jqXHR, | ||
textStatus: textStatus, | ||
errorThrown: errorThrown | ||
}); | ||
}; | ||
} | ||
return ajax; | ||
@@ -84,0 +116,0 @@ |
{ | ||
"name": "ic-ajax", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "ember friendly $.ajax wrapper", | ||
"main": "main.js", | ||
"devDependencies": { | ||
"testem": "~0.5.12" | ||
"testem": "~0.5.12", | ||
"prompt": "~0.2.11" | ||
}, | ||
@@ -26,2 +27,2 @@ "scripts": { | ||
"homepage": "https://github.com/instructure/ic-ajax" | ||
} | ||
} |
@@ -23,5 +23,15 @@ ic-ajax | ||
- AMD | ||
`define(['bower_components/ic-ajax'], function(ajax) {});` | ||
- Node.JS (CJS) | ||
`var ajax = require('ic-ajax')` | ||
- Globals | ||
`var ajax = ic.ajax;` | ||
All instructure canvas stuff lives on the `ic` global. | ||
API | ||
@@ -40,5 +50,6 @@ --- | ||
```js | ||
var ajax = ic.ajax; | ||
App.ApplicationRoute = Ember.Route.extend({ | ||
model: function() { | ||
return ic.ajax('/foo').then(function(result) { | ||
return ajax('/foo').then(function(result) { | ||
// result.response | ||
@@ -45,0 +56,0 @@ // result.textStatus |
8495
48.67%6
20%101
46.38%107
11.46%2
100%