Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom dataType updates #145

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ _.extend(Base.prototype, BBEvents, {
var extraProperties = this.extraProperties;
var triggers = [];
var changing, changes, newType, newVal, def, cast, err, attr,
attrs, dataType, silent, unset, currentVal, initial, hasChanged, isEqual;
attrs, dataType, silent, unset, currentVal, initial, hasChanged, isEqual, onSet;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (_.isObject(key) || key === null) {
Expand Down Expand Up @@ -156,11 +156,12 @@ _.extend(Base.prototype, BBEvents, {
}

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

// check type if we have one
if (dataType && dataType.set) {
cast = dataType.set(newVal);
cast = dataType.set.call(this, newVal, options);
newVal = cast.val;
newType = cast.type;
}
Expand Down Expand Up @@ -202,6 +203,7 @@ _.extend(Base.prototype, BBEvents, {
if (hasChanged) {
changes.push({prev: currentVal, val: newVal, key: attr});
self._changed[attr] = newVal;
onSet(newVal,currentVal, attr);
} else {
delete self._changed[attr];
}
Expand Down Expand Up @@ -335,6 +337,12 @@ _.extend(Base.prototype, BBEvents, {
return _.isEqual;
},

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

// Run validation against the next complete set of model attributes,
// returning `true` if all is well. Otherwise, fire an `"invalid"` event.
_validate: function (attrs, options) {
Expand Down Expand Up @@ -667,22 +675,21 @@ var dataTypes = {
};
}
},
compare: function (currentVal, newVal, attributeName) {
var isSame = currentVal === newVal;
compare: function (currentVal, newVal) {
return currentVal === newVal;
},

onSet : function(newVal, currentVal, attributeName){
// if this has changed we want to also handle
// event propagation
if (!isSame) {
if (currentVal) {
this.stopListening(currentVal);
}

if (newVal != null) {
this.listenTo(newVal, 'all', this._getEventBubblingHandler(attributeName));
}
if (currentVal) {
this.stopListening(currentVal);
}

return isSame;
if (newVal != null) {
this.listenTo(newVal, 'all', this._getEventBubblingHandler(attributeName));
}
}
}
};
Expand Down
53 changes: 46 additions & 7 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,19 +1579,19 @@ test('#118 setOnce can be used with default string', function (t) {

t.throws(function () {
tr.timezone = 'new thing';
}, 'since it has a default, this should throw');
}, 'since it has a default, this should throw');


var tr2;

t.doesNotThrow(function () {
tr2 = new TimeRange({timezone: 'my thing'});
}, 'if we set on init, should overwrite default');

t.throws(function () {
tr.timezone = 'new thing';
}, 'should now fail since its been set');
tr.timezone = 'new thing';
}, 'should now fail since its been set');

var OtherTimeRange = State.extend({
props: {
timezone: {
Expand All @@ -1605,11 +1605,50 @@ test('#118 setOnce can be used with default string', function (t) {

t.doesNotThrow(function () {
tr.timezone = 'thing';
}, 'should not throw first time');
}, 'should not throw first time');

t.throws(function () {
tr.timezone = 'other thing';
}, 'throws second time');

t.end();
});

test("#112 - should not set up events on child state if setOnce check fails", function(t){
var Person = State.extend({
props : {
birthday : {
type : 'state',
setOnce : true
}
}
});

var Birthday = State.extend({
props : {
day : 'date'
}
});
var p = new Person();
var bday = new Birthday({day : new Date()});
p.once('change:birthday', function() {
t.pass('birthday can change once');
});
p.birthday = bday;
var newBday = new Birthday({day : new Date()});
t.throws(function() {
p.birthday = newBday;

}, TypeError, 'Throws error on change of setOnce');

p.on('change:birthday.day', function() {
t.fail('should not trigger change event on old one');
});

newBday.day = new Date(1);


t.end();


});