Skip to content

Commit

Permalink
fixes mostly and once for mediator
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysbrettbowen committed Mar 19, 2012
1 parent 74223b8 commit 8854c14
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -208,6 +208,7 @@ you can then register your object with the mediator and the messages that you ma
#### v0.9 #### #### v0.9 ####


- reworked bind for performance - reworked bind for performance
- mediator now has once method


#### v0.8 #### #### v0.8 ####


Expand Down
26 changes: 12 additions & 14 deletions collection.js
Expand Up @@ -20,20 +20,18 @@ goog.require('goog.events.EventTarget');
* @param {Object=} options * @param {Object=} options
*/ */
mvc.Collection = function(options) { mvc.Collection = function(options) {
goog.base(this, options);

var defaults = { var defaults = {
sync: null, 'comparator': options['comparator']||null,
comparator: null, 'modelType': options['modelType']||mvc.Model,
modelType: mvc.Model, 'models': options['models']||[]
models: [],
schema: null
}; };
goog.object.remove(options,'comparator');
goog.object.remove(options,'modelType');
goog.object.remove(options,'models');

goog.base(this, options);



if(options)
goog.object.extend(defaults, options);

this.sync_ = defaults.sync;
/** /**
* @private * @private
* @type {Array.<mvc.Model>} * @type {Array.<mvc.Model>}
Expand All @@ -43,14 +41,14 @@ mvc.Collection = function(options) {
* @private * @private
* @type {?function(mvc.Model, mvc.Model):number} * @type {?function(mvc.Model, mvc.Model):number}
*/ */
this.comparator_ = defaults.comparator; this.comparator_ = defaults['comparator'];


/** /**
* @private * @private
*/ */
this.modelType_ = defaults.modelType; this.modelType_ = defaults['modelType'];


goog.array.forEach(defaults.models, function(model) { goog.array.forEach(defaults['models'], function(model) {
this.add(model, undefined, true); this.add(model, undefined, true);
}, this); }, this);
}; };
Expand Down
18 changes: 16 additions & 2 deletions mediator.js
Expand Up @@ -20,8 +20,6 @@ mvc.Mediator = function() {
this.listeners_ = {}; this.listeners_ = {};
}; };


goog.addSingletonGetter(mvc.Mediator);



/** /**
* @param {Object} obj * @param {Object} obj
Expand Down Expand Up @@ -89,6 +87,22 @@ mvc.Mediator.prototype.on = function(message, handler) {
return goog.getUid(handler); return goog.getUid(handler);
}; };


/**
* this will only run the function the first time the message is given
*
* @param {string} message
* @param {Function} handler
*/
mvc.Mediator.prototype.once = function(message, handler) {
var uid;
var fn = goog.bind(function() {
handler.apply(this, Array.prototype.slice.call(arguments,0));
this.off(uid);
},this);
uid = this.on(message, fn);
return uid;
};

/** /**
* @param {number} uid * @param {number} uid
*/ */
Expand Down
20 changes: 9 additions & 11 deletions model.js
Expand Up @@ -30,11 +30,11 @@ mvc.Model = function(options) {


if(!options) if(!options)
options = {}; options = {};
options.attr = options.attr || {}; options['attr'] = options['attr'] || {};


goog.object.forEach(options, function(val, key) { goog.object.forEach(options, function(val, key) {
if(!goog.isDef(defaults[key])) if(!goog.isDef(defaults[key]))
defaults.attr[key] = val; defaults['attr'][key] = val;
}); });


goog.object.extend(defaults, options); goog.object.extend(defaults, options);
Expand All @@ -58,9 +58,9 @@ mvc.Model = function(options) {
* @private * @private
* @type {?mvc.model.Schema} * @type {?mvc.model.Schema}
*/ */
this.schema_ = defaults.schema || null; this.schema_ = defaults['schema'] || null;


this.sync_ = defaults.sync|| null; this.sync_ = defaults['sync'] || null;


this.bound_ = []; this.bound_ = [];
this.boundAll_ = {}; this.boundAll_ = {};
Expand All @@ -70,7 +70,7 @@ mvc.Model = function(options) {


this.cid_ = goog.getUid(this); this.cid_ = goog.getUid(this);


goog.object.forEach(defaults.attr, function(val, name) { goog.object.forEach(defaults['attr'], function(val, name) {
this.attr_[name] = val; this.attr_[name] = val;
}, this); }, this);


Expand Down Expand Up @@ -147,9 +147,6 @@ mvc.Model.prototype.set = function(key, val, silent) {
temp[key] = val; temp[key] = val;
key = temp; key = temp;
} }
if(!silent) {
this.prev_ = goog.object.clone(this.attr_);
}
goog.object.forEach(key, function(val, key) { goog.object.forEach(key, function(val, key) {
if(!this.schema_ || !goog.isDef(val)) { if(!this.schema_ || !goog.isDef(val)) {
this.attr_[key] = val; this.attr_[key] = val;
Expand All @@ -164,6 +161,7 @@ mvc.Model.prototype.set = function(key, val, silent) {
if(success) { if(success) {
if(!silent) { if(!silent) {
this.dispatchEvent(goog.events.EventType.CHANGE); this.dispatchEvent(goog.events.EventType.CHANGE);
this.prev_ = goog.object.clone(this.attr_);
} }
return true; return true;
} }
Expand Down Expand Up @@ -328,14 +326,14 @@ mvc.Model.prototype.getBinder = function(key) {


mvc.Model.prototype.change_ = function(e) { mvc.Model.prototype.change_ = function(e) {
var changes = this.getChanges(); var changes = this.getChanges();
goog.object.forEach(this.bound_, function(val, key) { goog.array.forEach(this.bound_, function(val) {
if(goog.array.some(val.attr, function(attr) { if(goog.array.some(val.attr, function(attr) {
return !!changes[attr]; return goog.array.contains(changes, attr);
})) { })) {
val.fn.apply(val.hn, goog.array.concat(goog.array.map(val.attr, val.fn.apply(val.hn, goog.array.concat(goog.array.map(val.attr,
function(attr) { function(attr) {
return this.get(attr); return this.get(attr);
}),[this])); },this)));
} }
}, this); }, this);
goog.object.forEach(this.boundAll_, function(val) { goog.object.forEach(this.boundAll_, function(val) {
Expand Down
2 changes: 1 addition & 1 deletion router.js
Expand Up @@ -47,7 +47,7 @@ mvc.Router.prototype.route = function(route, fn) {
/** /**
* @private * @private
*/ */
mvc.Router.prototype.onChange_ = function(e) { mvc.Router.prototype.onChange_ = function() {
var fragment = this.history_.getToken(); var fragment = this.history_.getToken();
goog.array.forEach(this.routes_ || [], function(route) { goog.array.forEach(this.routes_ || [], function(route) {
var args = route.route.exec(fragment); var args = route.route.exec(fragment);
Expand Down

0 comments on commit 8854c14

Please sign in to comment.