derickbailey/backbone.modelbinding — Issue #27: Checkbox model mapping...
Author: dswitzer
Created: 2011-10-10 21:54:31+00:00
State: open

Is there anyway to map a checkbox's checked true/false value to a specific model value?

I'm working in some legacy code where the value in the model doesn't map to true/false. We have a couple situations where true/false would actually map to something like 0/1.

I also have weird situation where I need to map "checked=true" to be a false-like value. While I could start mapping special attributes, I'm trying to keep the model clean.

What I need is someway to do something like:

// define true/false mappings
var map = {
"attributeName1": ["trueValue", "falseValue"];
, "attributeName2": ["trueValue", "falseValue"];
}

Backbone.ModelBinding.bind(this, null, map);

So, what would happen is if the "attributeName1" attribute is equal to "trueValue", the checkbox would be selected. If the user unchecks the "attributeName1" element, the value in the model would be set to the "falseValue" instead of false.

Maybe there's a way to do this. I know this model isn't ideal, but I'm working with a lot of legacy code that would be difficult to update.

Thanks!
-Dan