Skip to content

Commit

Permalink
rename onSet to onChange and only call when value changes
Browse files Browse the repository at this point in the history
Since the `dataType.set` function is called on every set, the name
`dataType.onSet` was a little confusing. Since the function is only
called when the value changes, `dataType.onChange` seemed more
appropriate.
  • Loading branch information
chesles committed Oct 30, 2015
1 parent 35405f9 commit f0fe21b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 12 additions & 10 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ assign(Base.prototype, Events, {
var self = this;
var extraProperties = this.extraProperties;
var changing, changes, newType, newVal, def, cast, err, attr,
attrs, dataType, silent, unset, currentVal, initial, hasChanged, isEqual, onSet;
attrs, dataType, silent, unset, currentVal, initial, hasChanged, isEqual, onChange;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (isObject(key) || key === null) {
Expand Down Expand Up @@ -172,7 +172,7 @@ assign(Base.prototype, Events, {
}

isEqual = this._getCompareForType(def.type);
onSet = this._getOnSetForType(def.type);
onChange = this._getOnChangeForType(def.type);
dataType = this._dataTypes[def.type];

// check type if we have one
Expand Down Expand Up @@ -219,7 +219,7 @@ assign(Base.prototype, Events, {
if (hasChanged) {
changes.push({prev: currentVal, val: newVal, key: attr});
self._changed[attr] = newVal;
onSet(newVal, currentVal, attr);
onChange(newVal, currentVal, attr);
} else {
delete self._changed[attr];
}
Expand Down Expand Up @@ -356,9 +356,9 @@ assign(Base.prototype, Events, {
return _isEqual; // if no compare function is defined, use _.isEqual
},

_getOnSetForType : function(type){
_getOnChangeForType : function(type){
var dataType = this._dataTypes[type];
if (dataType && dataType.onSet) return bind(dataType.onSet, this);
if (dataType && dataType.onChange) return bind(dataType.onChange, this);
return noop;
},

Expand Down Expand Up @@ -580,7 +580,6 @@ function createPropertyDefinition(object, name, desc, isSession) {
}
var value = this._values[name];
var typeDef = this._dataTypes[def.type];
var onSet = this._getOnSetForType(def.type);
if (typeof value !== 'undefined') {
if (typeDef && typeDef.get) {
value = typeDef.get(value);
Expand All @@ -589,7 +588,10 @@ function createPropertyDefinition(object, name, desc, isSession) {
}
var defaultValue = result(def, 'default');
this._values[name] = defaultValue;
onSet(defaultValue, value, name);
if (typeof defaultValue !== 'undefined') {
var onChange = this._getOnChangeForType(def.type);
onChange(defaultValue, value, name);
}
return defaultValue;
}
});
Expand Down Expand Up @@ -714,12 +716,12 @@ var dataTypes = {
return currentVal === newVal;
},

onSet : function(newVal, currentVal, attributeName){
onChange : function(newVal, previousVal, attributeName){
// if this has changed we want to also handle
// event propagation

if (currentVal) {
this.stopListening(currentVal);
if (previousVal) {
this.stopListening(previousVal);
}

if (newVal != null) {
Expand Down
6 changes: 3 additions & 3 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ test("#112 - should not set up events on child state if setOnce check fails", fu
t.end();
});

test('#112 - onSet should be called for default values', function (t) {
test('#112 - onChange should be called for default values', function (t) {
var Person = State.extend({
dataTypes: {
'custom-type': {
Expand All @@ -1860,11 +1860,11 @@ test('#112 - onSet should be called for default values', function (t) {
val: newVal
};
},
onSet: function (newVal, curVal, name) {
onChange: function (newVal, curVal, name) {
t.equal(newVal.value, 100, 'should get the default value as newVal');
t.equal(curVal, undefined, 'should get undefined as current value');
t.equal(name, 'strength', 'should get the attribute name');
t.pass('onSet was called');
t.pass('onChange was called');
}
}
},
Expand Down

0 comments on commit f0fe21b

Please sign in to comment.