gmac / backbone.epoxy

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

Error parsing bindings with `length` attribute #124

Open jekuno opened 9 years ago

jekuno commented 9 years ago

There's a bug in Epoxy with model attributes called length.

Let's say I have an app which stores measures of vegetables e.g. weight and length. The default Epoxy setup doesn't work: http://jsfiddle.net/7dddfuh8/0/

The error message in the provided example:

uncaught exception: Error parsing bindings: "value:weight,events:['keyup']" ReferenceError: weight is not defined

The same setup works when renaming all length attributes: http://jsfiddle.net/7dddfuh8/1/

jekuno commented 8 years ago

I suppose this issue is caused by the underscore library which is used by epoxy. Collection function such as _.each and _.map internally use an isArrayLike function which uses duck-typing to identify array like objects by looking for a length attribute.

This is what the documentation of http://underscorejs.org/#each says: Collection functions work on arrays, objects, and array-like objects such as arguments, NodeList and similar. But it works by duck-typing, so avoid passing objects with a numeric length property.

I worked around this issue by renaming the length attribute. What do you think about adding an according hint to the documentation?