Changed the createDefer config param to just be defer
This commit is contained in:
@ -5,26 +5,21 @@
|
||||
* It will also instruct the client to use Angular's $http service for it's ajax requests
|
||||
*/
|
||||
var AngularConnector = require('./lib/connectors/angular');
|
||||
var Transport = require('./lib/transport');
|
||||
var Client = require('./lib/client');
|
||||
|
||||
process.angular_build = true;
|
||||
|
||||
/* global angular */
|
||||
angular.module('elasticsearch.client', [])
|
||||
angular.module('elasticsearch', [])
|
||||
.factory('esFactory', ['$http', '$q', function ($http, $q) {
|
||||
|
||||
AngularConnector.prototype.$http = $http;
|
||||
AngularConnector.prototype.$q = $q;
|
||||
|
||||
// make the Transport return $q promisses instead
|
||||
Transport.createDefer = function () {
|
||||
return $q.defer();
|
||||
};
|
||||
|
||||
var factory = function (config) {
|
||||
config = config || {};
|
||||
config.connectionClass = AngularConnector;
|
||||
config.$http = $http;
|
||||
config.defer = function () {
|
||||
return $q.defer();
|
||||
};
|
||||
return new Client(config);
|
||||
};
|
||||
|
||||
|
||||
@ -1,12 +1,22 @@
|
||||
/* global jQuery */
|
||||
(function ($) {
|
||||
process.jquery_build = true;
|
||||
$.es = require('./elasticsearch');
|
||||
$.es.Transport.createDefer = function () {
|
||||
var es = require('./elasticsearch');
|
||||
|
||||
function defer() {
|
||||
var def = $.Deferred();
|
||||
// def.promise is usually a property (in normal implementations)
|
||||
// we override the promise to keep things working
|
||||
def.promise = def.promise();
|
||||
return def;
|
||||
}
|
||||
|
||||
$.es = es;
|
||||
$.es.Client = function (config) {
|
||||
config = config || {};
|
||||
config.defer = defer;
|
||||
config.$ = $;
|
||||
return new es.Client(config);
|
||||
};
|
||||
|
||||
}(jQuery));
|
||||
11
src/lib/connectors/angular.js
vendored
11
src/lib/connectors/angular.js
vendored
@ -12,12 +12,14 @@ var ConnectionFault = require('../errors').ConnectionFault;
|
||||
|
||||
function AngularConnector(host, config) {
|
||||
ConnectionAbstract.call(this, host, config);
|
||||
this.defer = config.defer;
|
||||
this.$http = config.$http;
|
||||
}
|
||||
_.inherits(AngularConnector, ConnectionAbstract);
|
||||
|
||||
AngularConnector.prototype.request = function (params, cb) {
|
||||
var abort = this.$q.defer();
|
||||
this.$http({
|
||||
var abort = this.defer();
|
||||
AngularConnector.$http({
|
||||
method: params.method,
|
||||
url: this.host.makeUrl(params),
|
||||
data: params.body,
|
||||
@ -34,8 +36,3 @@ AngularConnector.prototype.request = function (params, cb) {
|
||||
abort.resolve();
|
||||
};
|
||||
};
|
||||
|
||||
// must be overwritten before this connection can be used
|
||||
AngularConnector.prototype.$http = null;
|
||||
// required in order to provide abort functionality
|
||||
AngularConnector.prototype.$q = null;
|
||||
|
||||
@ -33,6 +33,10 @@ function Transport(config) {
|
||||
// setup requestTimeout default
|
||||
this.requestTimeout = config.hasOwnProperty('requestTimeout') ? config.requestTimeout : 30000;
|
||||
|
||||
if (config.hasOwnProperty('defer')) {
|
||||
this.defer = config.defer;
|
||||
}
|
||||
|
||||
// randomizeHosts option
|
||||
var randomizeHosts = config.hasOwnProperty('randomizeHosts') ? !!config.randomizeHosts : true;
|
||||
|
||||
@ -88,7 +92,7 @@ Transport.nodesToHostCallbacks = {
|
||||
main: require('./nodes_to_host')
|
||||
};
|
||||
|
||||
Transport.createDefer = function () {
|
||||
Transport.prototype.defer = function () {
|
||||
return when.defer();
|
||||
};
|
||||
|
||||
@ -276,7 +280,7 @@ Transport.prototype.request = function (params, cb) {
|
||||
abort: abortRequest
|
||||
};
|
||||
} else {
|
||||
defer = Transport.createDefer();
|
||||
defer = this.defer();
|
||||
ret = defer.promise;
|
||||
ret.abort = abortRequest;
|
||||
}
|
||||
|
||||
@ -212,10 +212,9 @@ describe('Transport Class', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('::createDefer', function () {
|
||||
describe('#defer', function () {
|
||||
it('returns a when.js promise by default', function () {
|
||||
|
||||
Transport.createDefer().constructor.should.be.exactly(when.defer().constructor);
|
||||
Transport.prototype.defer().constructor.should.be.exactly(when.defer().constructor);
|
||||
});
|
||||
});
|
||||
|
||||
@ -717,10 +716,10 @@ describe('Transport Class', function () {
|
||||
when.isPromise(ret).should.be.ok;
|
||||
ret.abort.should.have.type('function');
|
||||
});
|
||||
it('promise is always pulled from the defer created by this.createDefer()', function () {
|
||||
it('promise is always pulled from the defer created by this.defer()', function () {
|
||||
var fakePromise = {};
|
||||
var origCreate = Transport.createDefer;
|
||||
Transport.createDefer = function () {
|
||||
var origDefer = Transport.prototype.defer;
|
||||
Transport.prototype.defer = function () {
|
||||
return {
|
||||
resolve: _.noop,
|
||||
reject: _.noop,
|
||||
@ -730,7 +729,7 @@ describe('Transport Class', function () {
|
||||
var tran = new Transport({});
|
||||
shortCircuitRequest(tran);
|
||||
var ret = tran.request({});
|
||||
Transport.createDefer = origCreate;
|
||||
Transport.prototype.defer = origDefer;
|
||||
ret.should.be.exactly(fakePromise);
|
||||
ret.abort.should.have.type('function');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user