Permalink
Browse files

fixes mostly and once for mediator

  • Loading branch information...
1 parent 74223b8 commit 8854c144cb0d1b1c945c62e042f2c523a0bbf8f8 @rhysbrettbowen committed Mar 19, 2012
Showing with 39 additions and 28 deletions.
  1. +1 −0 README.md
  2. +12 −14 collection.js
  3. +16 −2 mediator.js
  4. +9 −11 model.js
  5. +1 −1 router.js
View
1 README.md
@@ -208,6 +208,7 @@ you can then register your object with the mediator and the messages that you ma
#### v0.9 ####
- reworked bind for performance
+- mediator now has once method
#### v0.8 ####
View
26 collection.js
@@ -20,20 +20,18 @@ goog.require('goog.events.EventTarget');
* @param {Object=} options
*/
mvc.Collection = function(options) {
- goog.base(this, options);
-
var defaults = {
- sync: null,
- comparator: null,
- modelType: mvc.Model,
- models: [],
- schema: null
+ 'comparator': options['comparator']||null,
+ 'modelType': options['modelType']||mvc.Model,
+ 'models': options['models']||[]
};
+ 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
* @type {Array.<mvc.Model>}
@@ -43,14 +41,14 @@ mvc.Collection = function(options) {
* @private
* @type {?function(mvc.Model, mvc.Model):number}
*/
- this.comparator_ = defaults.comparator;
+ this.comparator_ = defaults['comparator'];
/**
* @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);
};
View
18 mediator.js
@@ -20,8 +20,6 @@ mvc.Mediator = function() {
this.listeners_ = {};
};
-goog.addSingletonGetter(mvc.Mediator);
-
/**
* @param {Object} obj
@@ -90,6 +88,22 @@ mvc.Mediator.prototype.on = function(message, 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
*/
mvc.Mediator.prototype.off = function(uid) {
View
20 model.js
@@ -30,11 +30,11 @@ mvc.Model = function(options) {
if(!options)
options = {};
- options.attr = options.attr || {};
+ options['attr'] = options['attr'] || {};
goog.object.forEach(options, function(val, key) {
if(!goog.isDef(defaults[key]))
- defaults.attr[key] = val;
+ defaults['attr'][key] = val;
});
goog.object.extend(defaults, options);
@@ -58,9 +58,9 @@ mvc.Model = function(options) {
* @private
* @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.boundAll_ = {};
@@ -70,7 +70,7 @@ mvc.Model = function(options) {
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);
@@ -147,9 +147,6 @@ mvc.Model.prototype.set = function(key, val, silent) {
temp[key] = val;
key = temp;
}
- if(!silent) {
- this.prev_ = goog.object.clone(this.attr_);
- }
goog.object.forEach(key, function(val, key) {
if(!this.schema_ || !goog.isDef(val)) {
this.attr_[key] = val;
@@ -164,6 +161,7 @@ mvc.Model.prototype.set = function(key, val, silent) {
if(success) {
if(!silent) {
this.dispatchEvent(goog.events.EventType.CHANGE);
+ this.prev_ = goog.object.clone(this.attr_);
}
return true;
}
@@ -328,14 +326,14 @@ mvc.Model.prototype.getBinder = function(key) {
mvc.Model.prototype.change_ = function(e) {
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) {
- return !!changes[attr];
+ return goog.array.contains(changes, attr);
})) {
val.fn.apply(val.hn, goog.array.concat(goog.array.map(val.attr,
function(attr) {
return this.get(attr);
- }),[this]));
+ },this)));
}
}, this);
goog.object.forEach(this.boundAll_, function(val) {
View
2 router.js
@@ -47,7 +47,7 @@ mvc.Router.prototype.route = function(route, fn) {
/**
* @private
*/
-mvc.Router.prototype.onChange_ = function(e) {
+mvc.Router.prototype.onChange_ = function() {
var fragment = this.history_.getToken();
goog.array.forEach(this.routes_ || [], function(route) {
var args = route.route.exec(fragment);

0 comments on commit 8854c14

Please sign in to comment.