Permalink
Browse files

Merge branch 'master' of github.com:taskrabbit/sudojs

  • Loading branch information...
2 parents f5c43e0 + d855900 commit 1b2c1824bada55b28928e1f0c8036905d1dcc329 @robrobbins robrobbins committed Mar 22, 2013
Showing with 87 additions and 16 deletions.
  1. +34 −8 build/debug/sudo-x.js
  2. +1 −1 build/debug/sudo.js
  3. +33 −7 extras/delegates/change/change.js
  4. +19 −0 extras/delegates/change/spec/change.spec.js
View
@@ -1665,26 +1665,52 @@ sudo.delegates.Change = function(data) {
};
// Delegates inherit from Model
sudo.delegates.Change.prototype = Object.create(sudo.Model.prototype);
+// ###addFilter
+// Place an entry into this object's hash of filters
+//
+// `param` {string} `key`
+// `param` {string} `val`
+// `returns` {object} this
+sudo.delegates.Change.prototype.addFilter = function addFilter(key, val) {
+ this.data.filters[key] = val;
+ return this;
+};
+// ###filter
// Change records are delivered here and filtered, calling any matching
// methods specified in `this.get('filters').
//
// `returns` {Object} a call to the specified _delegator_ method, passing
// a hash containing:
// 1. the `type` of Change
-// 2. the value located at the key/path
-// 3. the `oldValue` of the key if present
-sudo.delegates.Change.prototype.filter = function(change) {
- var filters = this.data.filters, name = change.name, obj = {};
+// 2. the `name` of the changed property
+// 3. the value located at the key/path
+// 4. the `oldValue` of the key if present
+sudo.delegates.Change.prototype.filter = function filter(change) {
+ var filters = this.data.filters, name = change.name,
+ type = change.type, obj = {};
// does my delegator care about this?
if(name in filters && filters.hasOwnProperty(name)) {
// assemble the object to return to the method
- obj.type = change.type;
- obj.value = name.indexOf('.') === -1 ? change.object[change.name] :
- sudo.getPath(name, change.object);
+ obj.type = type;
+ obj.name = name;
obj.oldValue = change.oldValue;
+ // delete operations will not have any value so no need to look
+ if(type !== 'deleted') {
+ obj.value = name.indexOf('.') === -1 ? change.object[change.name] :
+ sudo.getPath(name, change.object);
+ }
return this.delegator[filters[name]].call(this.delegator, obj);
}
};
+// ###removeFilter
+// Remove an entry from this object's hash of filters
+//
+// `param` {string} `key`
+// `returns` {object} this
+sudo.delegates.Change.prototype.removeFilter = function removeFilter(key) {
+ delete this.data.filters[key];
+ return this;
+};
// `private`
sudo.delegates.Change.prototype.role = 'change';
//##Data Delegate
@@ -1729,7 +1755,7 @@ sudo.delegates.Data.prototype.filter = function(obj) {
// `private`
sudo.delegates.Data.prototype.role = 'data';
-sudo.version = "0.9.3";
+sudo.version = "0.9.4";
window.sudo = sudo;
if(typeof window._ === "undefined") window._ = sudo;
}).call(this, this);
View
@@ -811,7 +811,7 @@ sudo.extensions.observable = {
return this.deliverChangeRecords();
}
};
-sudo.version = "0.9.3";
+sudo.version = "0.9.4";
window.sudo = sudo;
if(typeof window._ === "undefined") window._ = sudo;
}).call(this, this);
@@ -13,25 +13,51 @@ sudo.delegates.Change = function(data) {
};
// Delegates inherit from Model
sudo.delegates.Change.prototype = Object.create(sudo.Model.prototype);
+// ###addFilter
+// Place an entry into this object's hash of filters
+//
+// `param` {string} `key`
+// `param` {string} `val`
+// `returns` {object} this
+sudo.delegates.Change.prototype.addFilter = function addFilter(key, val) {
+ this.data.filters[key] = val;
+ return this;
+};
+// ###filter
// Change records are delivered here and filtered, calling any matching
// methods specified in `this.get('filters').
//
// `returns` {Object} a call to the specified _delegator_ method, passing
// a hash containing:
// 1. the `type` of Change
-// 2. the value located at the key/path
-// 3. the `oldValue` of the key if present
-sudo.delegates.Change.prototype.filter = function(change) {
- var filters = this.data.filters, name = change.name, obj = {};
+// 2. the `name` of the changed property
+// 3. the value located at the key/path
+// 4. the `oldValue` of the key if present
+sudo.delegates.Change.prototype.filter = function filter(change) {
+ var filters = this.data.filters, name = change.name,
+ type = change.type, obj = {};
// does my delegator care about this?
if(name in filters && filters.hasOwnProperty(name)) {
// assemble the object to return to the method
- obj.type = change.type;
- obj.value = name.indexOf('.') === -1 ? change.object[change.name] :
- sudo.getPath(name, change.object);
+ obj.type = type;
+ obj.name = name;
obj.oldValue = change.oldValue;
+ // delete operations will not have any value so no need to look
+ if(type !== 'deleted') {
+ obj.value = name.indexOf('.') === -1 ? change.object[change.name] :
+ sudo.getPath(name, change.object);
+ }
return this.delegator[filters[name]].call(this.delegator, obj);
}
};
+// ###removeFilter
+// Remove an entry from this object's hash of filters
+//
+// `param` {string} `key`
+// `returns` {object} this
+sudo.delegates.Change.prototype.removeFilter = function removeFilter(key) {
+ delete this.data.filters[key];
+ return this;
+};
// `private`
sudo.delegates.Change.prototype.role = 'change';
@@ -42,6 +42,25 @@ describe('Sudo Change Delegate', function() {
expect(spy).not.toHaveBeenCalled();
});
+ it('observes unset', function() {
+ model.setPath('is.messiah', true);
+ var spy = spyOn(view, 'denyDivinity');
+ model.unsetPath('is.messiah');
+ expect(spy).toHaveBeenCalled();
+ });
+
+ it('can add a filter', function() {
+ var del = view.delegate('change');
+ del.addFilter('isNaughty', 'acceptIt');
+ expect(del.get('filters').isNaughty).toBe('acceptIt');
+ });
+
+ it('can remove a filter', function() {
+ var del = view.delegate('change');
+ del.removeFilter('isNaughty');
+ expect(del.get('filters').isNaughty).toBeFalsy();
+ });
+
it('removes a delegate', function() {
var del = view.delegate('change');
view.removeDelegate('change');

0 comments on commit 1b2c182

Please sign in to comment.