Moving event to use Angular event's system

This commit is contained in:
Chris Cowan 2014-08-01 15:16:20 -07:00
parent f6159de406
commit 4a81f871ca
3 changed files with 0 additions and 157 deletions

View file

@ -1,84 +0,0 @@
define(function (require) {
var _ = require('lodash');
var rison = require('utils/rison');
return function ModelProvider() {
function Model(attributes) {
// Set the attributes or default to an empty object
this._listners = {};
_.assign(this, attributes);
}
/**
* Returns the attirbutes for the model
* @returns {object}
*/
Model.prototype.toObject = function () {
// return just the data.
return _.omit(this, function (value, key) {
return key.charAt(0) === '_' || _.isFunction(value);
});
};
/**
* Serialize the model to RISON
* @returns {string}
*/
Model.prototype.toRISON = function () {
return rison.encode(this.toObject());
};
/**
* Serialize the model to JSON
* @returns {object}
*/
Model.prototype.toJSON = function () {
return this.toObject();
};
/**
* Adds a listner
* @param {string} name The name of the event
* @returns {void}
*/
Model.prototype.on = function (name, listner) {
if (!_.isArray(this._listners[name])) {
this._listners[name] = [];
}
this._listners[name].push(listner);
};
/**
* Removes listener... if listner is empty then it removes all the events
* @param {string} name The name of the event
* @param {function} [listner]
* @returns {void}
*/
Model.prototype.off = function (name, listner) {
if (!listner) return delete this._listners[name];
this._listners = _.filter(this._listners[name], function (fn) {
return fn !== listner;
});
};
/**
* Emits and event
* @param {string} name The name of the event
* @param {mixed} [args...] Arguments pass to the listners
* @returns {void}
*/
Model.prototype.emit = function () {
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
if (this._listners[name]) {
_.each(this._listners[name], function (fn) {
fn.apply(null, args);
});
}
};
return Model;
};
});

View file

@ -8,7 +8,6 @@ define(function (require) {
function AppState(defaults) {
AppState.Super.call(this, '_a', defaults);
}
_.inherits(AppState, State);
return AppState;
};

View file

@ -1,72 +0,0 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var _ = require('lodash');
var sinon = require('sinon/sinon');
require('services/private');
// Load kibana
require('index');
describe('State Management', function () {
describe('Model', function () {
var $rootScope;
var Model;
beforeEach(function () {
module('kibana');
inject(function (_$rootScope_, Private) {
$rootScope = _$rootScope_;
Model = Private(require('components/state_management/_base_model'));
});
});
it('should take an inital set of values', function () {
var model = new Model({ message: 'test' });
expect(model).to.have.property('message', 'test');
});
it('should trigger $on events', function (done) {
var model = new Model();
var stub = sinon.stub();
model.on('test', stub);
model.emit('test');
sinon.assert.calledOnce(stub);
done();
});
it('should be extendable ($on/$emit)', function (done) {
function MyModel() {
MyModel.Super.call(this);
}
_.inherits(MyModel, Model);
var model = new MyModel();
var stub = sinon.stub();
model.on('test', stub);
model.emit('test');
sinon.assert.calledOnce(stub);
done();
});
it('should serialize _attributes to RISON', function () {
var model = new Model();
model.message = 'Testing... 1234';
var rison = model.toRISON();
expect(rison).to.equal('(message:\'Testing... 1234\')');
});
it('should serialize _attributes for JSON', function () {
var model = new Model();
model.message = 'Testing... 1234';
var json = JSON.stringify(model);
expect(json).to.equal('{"message":"Testing... 1234"}');
});
});
});
});