Skip to content

Commit

Permalink
moving to miso.events
Browse files Browse the repository at this point in the history
  • Loading branch information
Irene Ros committed Nov 16, 2012
1 parent e0f2f61 commit 8013cb9
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 214 deletions.
8 changes: 6 additions & 2 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = function(grunt) {
"lib/lodash.js",
"lib/underscore.math.js",
"lib/underscore.deferred.js",
"lib/miso.events.js",
"dist/miso.ds.<%= pkg.version %>.js"
]
},
Expand All @@ -78,6 +79,7 @@ module.exports = function(grunt) {
"lib/json2.js",
"lib/underscore.math.js",
"lib/underscore.deferred.js",
"lib/miso.events.js",
"dist/miso.ds.<%= pkg.version %>.js"
]
},
Expand Down Expand Up @@ -109,6 +111,10 @@ module.exports = function(grunt) {
"lib/underscore.deferred.js"
],

"dist/development/lib/miso.events.js" : [
"lib/miso.events.js"
],

buildstatus : {
dest : "dist/LASTBUILD",
src : [
Expand Down Expand Up @@ -201,8 +207,6 @@ module.exports = function(grunt) {

jshint : {
options : {
unused : true,
unuseds : true,
devel : true,
noempty : true,
forin : false,
Expand Down
119 changes: 119 additions & 0 deletions lib/miso.events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
(function(global, _) {

var Miso = global.Miso = (global.Miso || {});

/**
* Miso Events is a small set of methods that can be mixed into any object
* to make it evented. It allows one to then subscribe to specific object events,
* to publish events, unsubscribe and subscribeOnce.
*/
Miso.Events = {

/**
* Triggers a specific event and passes any additional arguments
* to the callbacks subscribed to that event.
* Params:
* name - the name of the event to trigger
* .* - any additional arguments to pass to callbacks.
*/
publish : function(name) {
var args = _.toArray(arguments);
args.shift();

if (this._events && this._events[name]) {
_.each(this._events[name], function(subscription) {
subscription.callback.apply(subscription.context || this, args);
}, this);
}
return this;
},

/**
* Allows subscribing on an evented object to a specific name.
* Provide a callback to trigger.
* Params:
* name - event to subscribe to
* callback - callback to trigger
* options - optional arguments
* priority - allows rearranging of existing callbacks based on priority
* context - allows attaching diff context to callback
* token - allows callback identification by token.
*/
subscribe : function(name, callback, options) {
options = options || {};
this._events = this._events || {};
this._events[name] = this._events[name] || [];

var subscription = {
callback : callback,
priority : options.priority || 0,
token : options.token || _.uniqueId('t'),
context : options.context || this
};
var position;
_.each(this._events[name], function(event, index) {
if (!_.isUndefined(position)) { return; }
if (event.priority <= subscription.priority) {
position = index;
}
});

this._events[name].splice(position, 0, subscription);
return subscription.token;
},

/**
* Allows subscribing to an event once. When the event is triggered
* this subscription will be removed.
* Params:
* name - name of event
* callback - The callback to trigger
* options - optional arguments
* priority - allows rearranging of existing callbacks based on priority
* context - allows attaching diff context to callback
* token - allows callback identification by token.
*/
subscribeOnce : function(name, callback, options) {
this._events = this._events || {};
options = options || {};
var self = this;

if (typeof options.token === "undefined") {
options.token = _.uniqueId('t');
}

return this.subscribe(name, function() {
self.unsubscribe(name, { token : options.token });
callback.apply(this, arguments);
}, options);
},

/**
* Allows unsubscribing from a specific event
* Params:
* name - event to unsubscribe from
* identifier - callback to remove OR token.
*/
unsubscribe : function(name, identifier) {

if (_.isUndefined(this._events[name])) { return this; }

if (_.isFunction(identifier)) {
this._events[name] = _.reject(this._events[name], function(b) {
return b.callback === identifier;
});

} else if ( _.isString(identifier)) {
this._events[name] = _.reject(this._events[name], function(b) {
return b.token === identifier;
});

} else {
this._events[name] = [];
}
return this;
}

};

}(this, _));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lodash": "0.9.0",
"moment": "1.7.2",
"underscore.deferred": "0.2.0",
"request": "2.9.153"
"request": "2.9.153",
"miso.events" : "*"
}
}
23 changes: 12 additions & 11 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version 0.0.1.2

(function(global, _, moment) {

var Miso = global.Miso || (global.Miso = {});
var Dataset = global.Miso.Dataset;

// take on miso dataview's prototype
Expand All @@ -25,7 +26,7 @@ Version 0.0.1.2
// is this a syncable dataset? if so, pull
// required methods and mark this as a syncable dataset.
if (options.sync === true) {
_.extend(this, Dataset.Events);
_.extend(this, Miso.Events);
this.syncable = true;
}

Expand Down Expand Up @@ -453,9 +454,9 @@ Version 0.0.1.2
}, this);

if (this.syncable && !options.silent) {
var e = this._buildEvent(deltas, this);
this.trigger('add', e );
this.trigger('change', e );
var e = Dataset.Events._buildEvent(deltas, this);
this.publish('add', e );
this.publish('change', e );
}

return this;
Expand Down Expand Up @@ -487,9 +488,9 @@ Version 0.0.1.2
}, this);

if (this.syncable && (!options || !options.silent)) {
var ev = this._buildEvent( deltas, this );
this.trigger('remove', ev );
this.trigger('change', ev );
var ev = Dataset.Events._buildEvent( deltas, this );
this.publish('remove', ev );
this.publish('change', ev );
}
},

Expand Down Expand Up @@ -589,9 +590,9 @@ Version 0.0.1.2
//computer column updates
//update triggers
if (this.syncable && (!options || !options.silent)) {
var ev = this._buildEvent( deltas, this );
this.trigger('update', ev );
this.trigger('change', ev );
var ev = Dataset.Events._buildEvent( deltas, this );
this.publish('update', ev );
this.publish('change', ev );
}
return this;
},
Expand All @@ -609,7 +610,7 @@ Version 0.0.1.2
});
this.length = 0;
if (this.syncable && (!options || !options.silent)) {
this.trigger("reset");
this.publish("reset");
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/derived.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(function(global, _) {

var Dataset = global.Miso.Dataset;
var Miso = global.Miso || (global.Miso = {});
var Dataset = Miso.Dataset;

/**
* A Miso.Derived dataset is a regular dataset that has been derived
Expand Down Expand Up @@ -40,9 +41,9 @@
});

if (this.parent.syncable) {
_.extend(this, Dataset.Events);
_.extend(this, Miso.Events);
this.syncable = true;
this.parent.bind("change", this._sync, this);
this.parent.subscribe("change", this._sync, { context : this });
}
};

Expand All @@ -51,11 +52,11 @@

// inherit all of dataset's methods.
_.extend(Dataset.Derived.prototype, {
_sync : function(event) {
_sync : function() {
// recompute the function on an event.
// TODO: would be nice to be more clever about this at some point.
this.func.call(this.args);
this.trigger("change");
this.publish("change");
}
});

Expand Down Expand Up @@ -102,7 +103,6 @@

// apply with the arguments columns, size, method
var computeMovingAverage = function() {
var win = [];

// normalize columns arg - if single string, to array it.
if (typeof columns === "string") {
Expand All @@ -115,7 +115,7 @@
.data.slice(size-1, this.parent.length);

// copy the columns we are NOT combining minus the sliced size.
this.eachColumn(function(columnName, column, i) {
this.eachColumn(function(columnName, column) {
if (columns.indexOf(columnName) === -1 && columnName !== "_oids") {
// copy data
column.data = this.parent.column(columnName).data.slice(size-1, this.parent.length);
Expand Down Expand Up @@ -258,7 +258,6 @@
// a cache of values
var categoryPositions = {},
categoryCount = 0,
byColumnPosition = this._columnPositionByName[byColumn],
originalByColumn = this.parent.column(byColumn);

// bin all values by their
Expand Down Expand Up @@ -297,7 +296,6 @@
_.each(columns, function(columnToGroup) {

var column = this.column(columnToGroup),
value = this.parent.column(columnToGroup).data[i],
binPosition = categoryPositions[category];

column.data[binPosition].push(this.parent.rowByPosition(i));
Expand Down
18 changes: 2 additions & 16 deletions src/node/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@ var moment = require("moment");
_.mixin(require("underscore.deferred"));
var request = require("request");

this.Miso = require("miso.events");

// Include underscore math
<%= underscoreMath %>

// Include Miso Dataset lib
<%= misoDataSet %>

// Load function that makes Miso plugin loading more formal.
this.Miso.load = function(moduleName) {
try {
// Attempt to load from node_modules
require(moduleName);
} catch (ex) {
// If path is not already full qualified prefix with cwd
if (!path.existsSync(moduleName)) {
moduleName = path.resolve(process.cwd(), moduleName);
}

// Load the correct module
require(moduleName);
}
};

// Ensure compatibility with Remote Importer
this.Miso.Xhr = function(options) {
// Make the request using the request module
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/google_spreadsheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
var positionRegex = /([A-Z]+)(\d+)/,
columnPositions = {};

_.each(data.feed.entry, function(cell, index) {
_.each(data.feed.entry, function(cell) {

var parts = positionRegex.exec(cell.title.$t),
column = parts[1],
Expand Down
9 changes: 5 additions & 4 deletions src/product.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(function(global, _) {

var Miso = global.Miso || (global.Miso = {});
var Dataset = global.Miso.Dataset;

/**
Expand Down Expand Up @@ -36,7 +37,7 @@
return this;
};

_.extend(Dataset.Product.prototype, Dataset.Events, {
_.extend(Dataset.Product.prototype, Miso.Events, {

/**
* return the raw value of the product
Expand Down Expand Up @@ -94,14 +95,14 @@
var delta = this._buildDelta(this.value, producer.call(_self));
this.value = delta.changed;
if (_self.syncable) {
var event = this._buildEvent(delta, this);
var event = Dataset.Events._buildEvent(delta, this);
if (!_.isUndefined(delta.old) && !options.silent && delta.old !== delta.changed) {
this.trigger("change", event);
this.publish("change", event);
}
}
}
});
this.bind("change", prod._sync, prod);
this.subscribe("change", prod._sync, { context : prod });
return prod;

} else {
Expand Down
Loading

0 comments on commit 8013cb9

Please sign in to comment.