derickbailey/backbone.modelbinding — Issue #45: Parse function for bound input fields
Author: paulyoder
Created: 2011-12-20 21:51:25+00:00
State: open

This feature will allow users to declaratively define a parse function to be called when a bound input field is changed.

One example for the need of a parse function is when a boolean attribute on a model is bound to 2 radio buttons, one radio value is 'true' and the other is 'false'. Currently the modelbinding will set the attribute to a string value of 'true' or 'false'. Having a parse function can convert the string to a boolean.

Another example is when an attribute that is expected to be a number is bound to a text field. The parse function can convert the string value to a number.

I'm currently doing this manually on a project, and it would be nice to have this functionality baked in.

I propose having the following declarative syntax

And the function declaration can be the following

function numberParser(value, attribute, model, element)

The parse function can either set the attribute value on the model itself (and return nothing) or it can return the parsed value and backbone.modelbinding will take care of setting the attribute to the returned value.