From 74d3183cc644dfb8d50d084fc9c26e5ef494d99e Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Tue, 17 Dec 2013 11:23:16 -0700 Subject: [PATCH] Changed the createDefer config param to just be defer --- src/elasticsearch.angular.js | 15 +++++---------- src/elasticsearch.jquery.js | 14 ++++++++++++-- src/lib/connectors/angular.js | 11 ++++------- src/lib/transport.js | 8 ++++++-- test/unit/test_transport.js | 13 ++++++------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/elasticsearch.angular.js b/src/elasticsearch.angular.js index 6ace08e0e..0bfa86952 100644 --- a/src/elasticsearch.angular.js +++ b/src/elasticsearch.angular.js @@ -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); }; diff --git a/src/elasticsearch.jquery.js b/src/elasticsearch.jquery.js index af780413c..a03380e01 100644 --- a/src/elasticsearch.jquery.js +++ b/src/elasticsearch.jquery.js @@ -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)); \ No newline at end of file diff --git a/src/lib/connectors/angular.js b/src/lib/connectors/angular.js index c66387903..3c5ecb9eb 100644 --- a/src/lib/connectors/angular.js +++ b/src/lib/connectors/angular.js @@ -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; diff --git a/src/lib/transport.js b/src/lib/transport.js index a484b4b66..e8cc58743 100644 --- a/src/lib/transport.js +++ b/src/lib/transport.js @@ -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; } diff --git a/test/unit/test_transport.js b/test/unit/test_transport.js index 60a384e3e..644e99e55 100644 --- a/test/unit/test_transport.js +++ b/test/unit/test_transport.js @@ -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'); });