Created
September 29, 2014 09:53
-
-
Save chrism/007ec2416789d78da77c to your computer and use it in GitHub Desktop.
ic-ajax for use in a JSBin with var ajax = ic.ajax; ajax.defineFixture & RESTAdapter.
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
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),(f.ic||(f.ic={})).ajax=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | |
"use strict"; | |
/*! | |
* ic-ajax | |
* | |
* - (c) 2013 Instructure, Inc | |
* - please see license at https://github.com/instructure/ic-ajax/blob/master/LICENSE | |
* - inspired by discourse ajax: https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/mixins/ajax.js#L19 | |
*/ | |
var Ember = window.Ember["default"] || window.Ember; | |
/* | |
* jQuery.ajax wrapper, supports the same signature except providing | |
* `success` and `error` handlers will throw an error (use promises instead) | |
* and it resolves only the response (no access to jqXHR or textStatus). | |
*/ | |
function request() { | |
return raw.apply(null, arguments).then(function(result) { | |
return result.response; | |
}, null, 'ic-ajax: unwrap raw ajax response'); | |
} | |
exports.request = request;request.OVERRIDE_REST_ADAPTER = true; | |
exports["default"] = request; | |
/* | |
* Same as `request` except it resolves an object with `{response, textStatus, | |
* jqXHR}`, useful if you need access to the jqXHR object for headers, etc. | |
*/ | |
function raw() { | |
return makePromise(parseArgs.apply(null, arguments)); | |
} | |
exports.raw = raw;var __fixtures__ = {}; | |
exports.__fixtures__ = __fixtures__; | |
/* | |
* 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: | |
* | |
* defineFixture('/self', { | |
* response: { firstName: 'Ryan', lastName: 'Florence' }, | |
* textStatus: 'success' | |
* jqXHR: {} | |
* }); | |
* | |
* @param {String} url | |
* @param {Object} fixture | |
*/ | |
function defineFixture(url, fixture) { | |
__fixtures__[url] = JSON.parse(JSON.stringify(fixture)); | |
} | |
exports.defineFixture = defineFixture;/* | |
* Looks up a fixture by url. | |
* | |
* @param {String} url | |
*/ | |
function lookupFixture (url) { | |
return __fixtures__ && __fixtures__[url]; | |
} | |
exports.lookupFixture = lookupFixture;function makePromise(settings) { | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
var fixture = lookupFixture(settings.url); | |
if (fixture) { | |
if (fixture.textStatus === 'success') { | |
return Ember.run(null, resolve, fixture); | |
} else { | |
return Ember.run(null, reject, fixture); | |
} | |
} | |
settings.success = makeSuccess(resolve); | |
settings.error = makeError(reject); | |
Ember.$.ajax(settings); | |
}, 'ic-ajax: ' + (settings.type || 'GET') + ' to ' + settings.url); | |
}; | |
function parseArgs() { | |
var settings = {}; | |
if (arguments.length === 1) { | |
if (typeof arguments[0] === "string") { | |
settings.url = arguments[0]; | |
} else { | |
settings = arguments[0]; | |
} | |
} else if (arguments.length === 2) { | |
settings = arguments[1]; | |
settings.url = arguments[0]; | |
} | |
if (settings.success || settings.error) { | |
throw new Ember.Error("ajax should use promises, received 'success' or 'error' callback"); | |
} | |
return settings; | |
} | |
function makeSuccess(resolve) { | |
return function(response, textStatus, jqXHR) { | |
Ember.run(null, resolve, { | |
response: response, | |
textStatus: textStatus, | |
jqXHR: jqXHR | |
}); | |
} | |
} | |
function makeError(reject) { | |
return function(jqXHR, textStatus, errorThrown) { | |
Ember.run(null, reject, { | |
jqXHR: jqXHR, | |
textStatus: textStatus, | |
errorThrown: errorThrown | |
}); | |
}; | |
} | |
if (typeof window.DS !== 'undefined'){ | |
Ember.onLoad('Ember.Application', function(Application){ | |
Application.initializer({ | |
name: 'ic-ajax_REST_Adapter', | |
after: 'store', | |
initialize: function(container, application){ | |
if (request.OVERRIDE_REST_ADAPTER) { | |
DS.RESTAdapter.reopen({ | |
ajax: function(url, type, options){ | |
options = this.ajaxOptions(url, type, options); | |
return request(options); | |
} | |
}); | |
} | |
} | |
}); | |
}); | |
} | |
},{}]},{},[1]) | |
(1) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment