Last active
December 14, 2015 19:49
-
-
Save menacestudio/5139389 to your computer and use it in GitHub Desktop.
Backbone: Only persist a Model with valid attributes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var President = Backbone.Model.extend({}); | |
var m = new President({first: 'Abraham', last: 'Lincoln', age: 90, registered: true}); | |
m.set({first: null}); | |
/** Loop through each property and unset if invalid. Also check if property if boolean type. */ | |
_.each(m.toJSON(), function(val, col){ | |
if (typeof val !=='boolean' && !val) { | |
m.unset(col); | |
} | |
}, this); | |
console.log(m.toJSON()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var President = Backbone.Model.extend({}); | |
var m = new President({first: 'Abraham', last: 'Lincoln', age: 90, registered: true}); | |
m.set({first: null}); | |
/** Pass changed attributes to a new Model */ | |
var t = new President(); | |
if (!_.isEmpty(m.changedAttributes())) { | |
_.each(m.changedAttributes(), function(val, col) { | |
t.set(col, val); | |
}, this); | |
} | |
console.log(t.toJSON()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment