Removing isolated_scope and replacing it with events (PromiseEmitter)

This commit is contained in:
Chris Cowan 2014-08-04 18:11:50 -07:00
parent aa8981f2f7
commit 6897ceda63
7 changed files with 116 additions and 116 deletions

View file

@ -0,0 +1,50 @@
define(function (require) {
var _ = require('lodash');
return function EventsProvider(Private, PromiseEmitter) {
var BaseObject = Private(require('components/state_management/_base_object'));
_.inherits(Events, BaseObject);
function Events() {
Events.Super.call(this);
this._listeners = {};
}
/**
* Listens for events
* @param {string} name The name of the event
* @param {function} handler The handler for the event
* @returns {PromiseEmitter}
*/
Events.prototype.on = function (name, handler) {
var self = this;
if (!_.isArray(this._listeners[name])) {
this._listeners[name] = [];
}
return new PromiseEmitter(function (resolve, reject, defer) {
self._listeners[name].push(defer);
}, handler);
};
/**
* Emits and event using the PromiseEmitter
* @param {string} name The name of the event
* @param {mixed} args The args to pass along to the handers
* @returns {void}
*/
Events.prototype.emit = function () {
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
if (this._listeners[name]) {
_.each(this._listeners[name], function (defer) {
defer.resolve.apply(defer, args);
});
}
};
return Events;
};
});

View file

@ -1,31 +0,0 @@
define(function (require) {
var _ = require('lodash');
return function IsolatedScopeProvider($rootScope) {
var Scope = $rootScope.constructor;
_.inherits(IsolatedScope, Scope);
function IsolatedScope() {
IsolatedScope.Super.call(this);
// Prepare the new scope and attach it to the rootScope's linked list
this.$root = $rootScope.$root;
// ensure that there is just one async queue per $rootScope and its children
this.$$asyncQueue = $rootScope.$$asyncQueue;
this.$$postDigestQueue = $rootScope.$$postDigestQueue;
this['this'] = this;
this.$parent = $rootScope;
this.$$prevSibling = $rootScope.$$childTail;
if ($rootScope.$$childHead) {
$rootScope.$$childTail.$$nextSibling = this;
$rootScope.$$childTail = this;
} else {
$rootScope.$$childHead = $rootScope.$$childTail = this;
}
}
return IsolatedScope;
};
});

View file

@ -6,9 +6,9 @@ define(function (require) {
return function StateProvider(Private, $rootScope, $location) {
var BaseObject = Private(require('components/state_management/_base_object'));
var IsolatedScope = Private(require('components/state_management/_isolated_scope'));
var Events = Private(require('components/state_management/_events'));
_.inherits(State, IsolatedScope);
_.inherits(State, Events);
function State(urlParam, defaults) {
State.Super.call(this);
this._defaults = defaults || {};
@ -21,9 +21,6 @@ define(function (require) {
this.fetch();
}
// // mixin the base object methods
_.mixin(State.prototype, BaseObject.prototype);
/**
* Fetches the state from the url
* @returns {void}
@ -36,7 +33,7 @@ define(function (require) {
// it will change state in place.
var diffResults = applyDiff(this, stash);
if (diffResults.keys.length) {
this.$emit('fetch_with_changes', diffResults.keys);
this.emit('fetch_with_changes', diffResults.keys);
}
};
@ -53,7 +50,7 @@ define(function (require) {
// it will change stash in place.
var diffResults = applyDiff(stash, state);
if (diffResults.keys.length) {
this.$emit('save_with_changes', diffResults.keys);
this.emit('save_with_changes', diffResults.keys);
}
search[this._urlParam] = this.toRISON();
$location.search(search);
@ -76,14 +73,7 @@ define(function (require) {
* @returns {void}
*/
State.prototype.onUpdate = function (cb) {
this.$on('fetch_with_changes', function () {
// onUpdate interface for the listner expects the first argument to
// be the start of the arguments passed to the event emitter. So we need
// to lop off the first argument and pass the rest.
var args = Array.prototype.slice.call(arguments);
args.shift();
cb.apply(null, args);
});
this.on('fetch_with_changes', cb);
};
return State;

View file

@ -77,7 +77,7 @@
'specs/state_management/_base_object',
'specs/state_management/state',
'specs/utils/diff_object',
'specs/state_management/_isolated_scope'
'specs/state_management/_events'
], function (kibana, sinon) {
kibana.load(function () {
var xhr = sinon.useFakeXMLHttpRequest();

View file

@ -0,0 +1,51 @@
define(function (require) {
var angular = require('angular');
var _ = require('lodash');
var sinon = require('sinon/sinon');
require('services/private');
// Load kibana
require('index');
describe('State Management', function () {
describe('Events', function () {
var $rootScope;
var Events;
beforeEach(function () {
module('kibana');
inject(function (_$rootScope_, Private) {
$rootScope = _$rootScope_;
Events = Private(require('components/state_management/_events'));
});
});
it('should handle on events', function (done) {
var obj = new Events();
obj.on('test', function (message) {
expect(message).to.equal('Hello World');
done();
});
obj.emit('test', 'Hello World');
$rootScope.$apply();
});
it('should work with inherited objects', function (done) {
_.inherits(MyEventedObject, Events);
function MyEventedObject() {
MyEventedObject.Super.call(this);
}
var obj = new MyEventedObject();
obj.on('test', function (message) {
expect(message).to.equal('Hello World');
done();
});
obj.emit('test', 'Hello World');
$rootScope.$apply();
});
});
});
});

View file

@ -1,61 +0,0 @@
define(function (require) {
var angular = require('angular');
var _ = require('lodash');
var sinon = require('sinon/sinon');
require('services/private');
// Load kibana
require('index');
describe('State Management', function () {
describe('IsolatedScope', function () {
var $rootScope;
var IsolatedScope;
beforeEach(function () {
module('kibana');
inject(function (_$rootScope_, Private) {
$rootScope = _$rootScope_;
IsolatedScope = Private(require('components/state_management/_isolated_scope'));
});
});
it('should handle $on events', function () {
var scope = new IsolatedScope();
var stub = sinon.stub();
scope.$on('test', stub);
scope.$emit('test', 'Hello World');
sinon.assert.calledOnce(stub);
});
it('should handle $watch events', function () {
var scope = new IsolatedScope();
var stub = sinon.stub();
scope.$watch('test', stub);
scope.test = 'Hello World';
$rootScope.$apply();
sinon.assert.calledOnce(stub);
});
it('should work with inherited objects', function () {
_.inherits(MyScope, IsolatedScope);
function MyScope() {
MyScope.Super.call(this);
}
var scope = new MyScope();
var eventStub = sinon.stub();
var watchStub = sinon.stub();
scope.$on('test', eventStub);
scope.$emit('test', 'Hello World');
scope.$watch('test', watchStub);
scope.test = 'Hello World';
$rootScope.$apply();
sinon.assert.calledOnce(eventStub);
sinon.assert.calledOnce(watchStub);
});
});
});
});

View file

@ -10,7 +10,7 @@ define(function (require) {
describe('State Management', function () {
describe('State', function () {
var $rootScope, $location, State, IsolatedScope;
var $rootScope, $location, State, Events;
beforeEach(function () {
module('kibana');
@ -19,13 +19,13 @@ define(function (require) {
$location = _$location_;
$rootScope = _$rootScope_;
State = Private(require('components/state_management/state'));
IsolatedScope = Private(require('components/state_management/_isolated_scope'));
Events = Private(require('components/state_management/_events'));
});
});
it('should inherit from IsolatedScope', function () {
it('should inherit from Events', function () {
var state = new State();
expect(state).to.be.an(IsolatedScope);
expect(state).to.be.an(Events);
});
it('should save to $location.search()', function () {
@ -38,7 +38,7 @@ define(function (require) {
it('should emit an event if changes are saved', function (done) {
var state = new State();
state.$on('save_with_changes', function (event, keys) {
state.on('save_with_changes', function (keys) {
expect(keys).to.eql(['test']);
done();
});
@ -57,22 +57,23 @@ define(function (require) {
it('should emit an event if changes are fetched', function (done) {
var state = new State();
state.$on('fetch_with_changes', function (events, keys) {
state.on('fetch_with_changes', function (keys) {
expect(keys).to.eql(['foo']);
done();
});
$location.search({ _s: '(foo:bar)' });
state.fetch();
expect(state).to.have.property('foo', 'bar');
$rootScope.$apply();
});
it('should have events that attach to scope', function (done) {
var state = new State();
state.$on('test', function (event, message) {
state.on('test', function (message) {
expect(message).to.equal('foo');
done();
});
state.$emit('test', 'foo');
state.emit('test', 'foo');
$rootScope.$apply();
});