gmac / backbone.epoxy

Declarative data binding and computed models for Backbone
http://epoxyjs.org
MIT License
615 stars 89 forks source link

Structured model and data-binding #62

Closed hcapitaine closed 10 years ago

hcapitaine commented 10 years ago

Given that I have this kind of model:

{
     mainObject:{
         myProperty : "valueForMyProperty"
     }
}

and this DOM element:

<input id="objet" name="objet" data-bind="value:mainObject.myProperty"/>

My problem is that my element is not bound.

So I had a look into the code and found that the function bindElementToView is the one applying binding from declarations. If the declaration is a string it takes the declaration from the dom element. Everything is OK for now.

But in this method, this part of the code:

var parserFunct = bindingCache[declarations] || (bindingCache[declarations] = new Function('$f','$c','with($f){with($c){return{'+ declarations +'}}}'));
var bindings = parserFunct(filters, context); 

executes the function from context for each part of the binding, in our case: mainObj().property() Of course property doesn't exist.

I think a possible way coulb be to flatten the model the model and then access it the same way.

What do you think about it?

gmac commented 10 years ago

In general, deep binding doesn't work very well – meaning: binding to a property of a plain object stored within a model. The problem with attributes of a plain object is that they don't fire events. While you can read their values once, you won't get events off of them when they change, so the view has nothing to bind to. For this reason, just stick to binding to attributes (and computed attributes) of the bound model directly.