Last active
August 29, 2015 14:04
-
-
Save jasonmerino/a859e3b99c4196dded09 to your computer and use it in GitHub Desktop.
Extends Backbone.Model.set to conform given properties to the models schema before setting them on the model.
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
Backbone.Model.prototype.defaultSet = Backbone.Model.prototype.set; | |
_.extend(Backbone.Model.prototype, { | |
/** | |
* Adds schema type casting to models before setting attributes | |
* @param {Object|String} key The key of the value to set or an object of value pairs | |
* @param {String|Object} val The value to set or the options object | |
* @param {Object|undefined} options The options for saving or undefined | |
*/ | |
set: function(key, val, options) { | |
var attrs; | |
if (typeof key === 'object') { | |
attrs = key; | |
options = val; | |
} else { | |
(attrs = {})[key] = val; | |
} | |
if (this.schema) { | |
_.each(attrs, _.bind(function(v, k) { | |
try { | |
switch (this.schema[k]) { | |
case 'boolean': | |
attrs[k] = v === 'true' ? true : v === 'false' ? false : v; | |
break; | |
case 'int': | |
attrs[k] = parseInt(v, 10); | |
break; | |
case 'float': | |
attrs[k] = parseFloat(v); | |
break; | |
case 'string': | |
attrs[k] = v.toString(); | |
break; | |
default: | |
attrs[k] = v; | |
break; | |
} | |
} catch (error) {} | |
}, this)); | |
} | |
return this.defaultSet.call(this, attrs, options); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment