ampersand-state
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -51,3 +51,3 @@ var _ = require('underscore'); | ||
var changing, previous, changes, newType, newVal, def, cast, err, attr, | ||
attrs, dataType, silent, unset, currentVal, initial, hasChanged; | ||
attrs, dataType, silent, unset, currentVal, initial, hasChanged, isEqual; | ||
@@ -100,2 +100,3 @@ // Handle both `"key", value` and `{key: value}` -style arguments. | ||
isEqual = this._getCompareForType(def.type); | ||
dataType = this._dataTypes[def.type]; | ||
@@ -135,7 +136,3 @@ | ||
if (dataType && dataType.compare) { | ||
hasChanged = !dataType.compare(currentVal, newVal); | ||
} else { | ||
hasChanged = !_.isEqual(currentVal, newVal); | ||
} | ||
hasChanged = !isEqual(currentVal, newVal); | ||
@@ -153,3 +150,3 @@ // enforce `setOnce` for properties if set | ||
// keep track of changed attributes | ||
if (!_.isEqual(previous[attr], newVal)) { | ||
if (!isEqual(previous[attr], newVal)) { | ||
self._changed[attr] = newVal; | ||
@@ -255,4 +252,7 @@ } else { | ||
var old = this._changing ? this._previousAttributes : this.attributes; | ||
var def, isEqual; | ||
for (var attr in diff) { | ||
if (_.isEqual(old[attr], (val = diff[attr]))) continue; | ||
def = this._definition[attr]; | ||
isEqual = this._getCompareForType(def && def.type); | ||
if (isEqual(old[attr], (val = diff[attr]))) continue; | ||
(changed || (changed = {}))[attr] = val; | ||
@@ -302,2 +302,9 @@ } | ||
// Determine which comparison algorithm to use for comparing a property | ||
_getCompareForType: function (type) { | ||
var dataType = this._dataTypes[type]; | ||
if (dataType && dataType.compare) return dataType.compare; | ||
return _.isEqual; | ||
}, | ||
// Run validation against the next complete set of model attributes, | ||
@@ -304,0 +311,0 @@ // returning `true` if all is well. Otherwise, fire an `"invalid"` event. |
{ | ||
"name": "ampersand-state", | ||
"description": "An observable, extensible state object with derived watchable properties.", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -527,2 +527,38 @@ var tape = require('tape'); | ||
test('Uses dataType compare', function (t) { | ||
var compareRun; | ||
var Foo = State.extend({ | ||
props: { | ||
silliness: 'crazyType' | ||
}, | ||
dataTypes: { | ||
crazyType: { | ||
compare: function (oldVal, newVal) { | ||
compareRun = true; | ||
return false; | ||
}, | ||
set: function (newVal) { | ||
return { | ||
val: newVal, | ||
type: 'crazyType' | ||
}; | ||
}, | ||
get: function (val) { | ||
return val + 'crazy!'; | ||
} | ||
} | ||
} | ||
}); | ||
compareRun = false; | ||
var foo = new Foo({ silliness: 'you' }); | ||
t.assert(compareRun); | ||
compareRun = false; | ||
foo.silliness = 'they'; | ||
t.assert(compareRun); | ||
t.end(); | ||
}); | ||
test('Should only allow nulls where specified', function (t) { | ||
@@ -529,0 +565,0 @@ var foo = new Foo({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57183
1394