javascript - Looping over attributes in validation fails to stop set of attribute -
normally in backbone validation, have crazy amount of if()
statements, i've seen in many other code samples well. validation pretty crap shoot in backbone; however, if()
way seems work. want clean code bit , wrote code return error
should stop backbone saving attribute, doesn't!
old code works
validate : function(attr){ if(attr.firstname){ var defaultvalue = 'first name', value = attr.firstname.tolowercase(); if(value == defaultvalue){ return 'error'; } } }
new code doesn't work
//my default strings place myapp.strings.defaults = { firstname : 'first name' } //model validate function validate : function(attr){ jquery.each(attr, function(key, value){ var defaultvalue = myapp.strings.defaults[key]; if(defaultvalue){ defaultvalue = jquery.trim(defaultvalue.tolowercase()); if(value == defaultvalue){ console.log(value, defaultvalue); //fires, , outputs both being same return 'error'; } } }); }
are not allowed loop on attributes in backbone validation?
you not returning value validate method, returning 'error'
each()
callback method, not validate
//my default strings place myapp.strings.defaults = { firstname : 'first name' } //model validate function validate : function(attr){ var error; jquery.each(attr, function(key, value){ var defaultvalue = myapp.strings.defaults[key]; if(defaultvalue){ defaultvalue = jquery.trim(defaultvalue.tolowercase()); if(value.tolowercase() == defaultvalue){ console.log(value, defaultvalue); //fires, , outputs both being same error = 'error'; return false; } } }); return error; }
Comments
Post a Comment