backbone.modelbinding icon indicating copy to clipboard operation
backbone.modelbinding copied to clipboard

Simple change to provide more flexibility for attributes

Open tobowers opened this issue 13 years ago • 0 comments

This is a simple change that could be the start of something larger. I had seen others talking about rails integration and the object[attr] names there. Rather than a major refactor, this small fix allowed me to monkey patch by overriding Configuration.getBindingValue as so:

Backbone.ModelBinding.Configuration.prototype.getBindingValue = (element, type) ->
  bindingAttr = this.getBindingAttr(type)
  # looks for the railsish object[attribute] style
  attributeRegex = /^[^\[]+\[(.+)\]$/
  attributeValue = element.attr(bindingAttr)
  if attributeRegex.test(attributeValue)
    return attributeValue.match(attributeRegex)[1]
  else
    return attributeValue

To be clear: the above change is not in my commit - just changing where functions are assigned. It should save a little memory on every config instance too.

tobowers avatar Feb 07 '12 14:02 tobowers