Skip to content

Commit

Permalink
Merge branch 'master' of github.com:epitome-mvc/Epitome
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitarChristoff committed Dec 26, 2013
2 parents 141b2a9 + 7167734 commit 6c46d94
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/epitome-model.js
Expand Up @@ -2,6 +2,7 @@
'use strict';

// wrapper function for requirejs or normal object
var undef = 'undefined';
var wrap = function(isEqual, Events){

return new Class({
Expand Down Expand Up @@ -68,11 +69,15 @@
// private, real setter functions, not on prototype, see note above
_set: function(key, value){
// needs to be bound the the instance.
if (!key || typeof value === 'undefined') return this;
if (!key || typeof value === undef) return this;

// custom setter - see bit further down
if (this.properties[key] && this.properties[key]['set'])
return this.properties[key]['set'].call(this, value);
if (this.properties[key] && this.properties[key]['set']) {
value = this.properties[key]['set'].call(this, value);//if value is returned by setter proceed to set the new value normally
if(typeof value === undef) {
return;
}
}

// no change? this is crude and works for primitives.
if (this._attributes[key] && isEqual(this._attributes[key], value))
Expand Down Expand Up @@ -117,7 +122,7 @@
}

// else, return from attributes or return null when undefined.
return (key && typeof this._attributes[key] !== 'undefined') ? this._attributes[key] : null;
return (key && typeof this._attributes[key] !== undef) ? this._attributes[key] : null;
}.overloadGetter(),

unset: function(){
Expand Down Expand Up @@ -175,7 +180,7 @@
if (typeof define === 'function' && define.amd){
define(['./epitome-isequal', './epitome-events'], wrap);
}
else if (typeof module !== 'undefined' && module.exports){
else if (typeof module !== undef && module.exports){
require('mootools');
module.exports = wrap(require('./epitome-isequal'), require('./epitome-events'));
}
Expand Down
42 changes: 42 additions & 0 deletions test/tests/epitome-model-test.js
Expand Up @@ -224,6 +224,48 @@ buster.testCase('Basic Epitome model creation with initial data >', {
});

this.model.empty();
},

'Expect model `set` with custom property setter to override value and continue setting if anything is returned >': function() {
var spy = this.spy();

var pre = this.model.get('foo');
this.model.properties = Object.merge(this.model.properties, {
foo: {
set: function(val) {
//do nothing so dont change
}
}
});

this.model.set('foo', 'something');
buster.assert.same(this.model.get('foo'), pre);

this.model.properties = Object.merge(this.model.properties, {
foo: {
set: function(val) {
return 'my ' + val;
}
}
});

this.model.set('foo', 'bar');
buster.assert.same(this.model.get('foo'), 'my bar');
},

'Expect returning a non truthy value (besides undef) to set the property >': function() {
var spy = this.spy();

this.model.properties = Object.merge({
foo: {
set: function(val) {
return false;
}
}
}, this.model.properties);

this.model.set('foo', 'bar');
buster.assert.same(this.model.get('foo'), false);
}

});
Expand Down

0 comments on commit 6c46d94

Please sign in to comment.