leonidas / transparency

Transparency is a semantic template engine for the browser. It maps JSON objects to DOM elements by id, class and data-bind attributes.
http://leonidas.github.com/transparency/
MIT License
969 stars 112 forks source link

easier binding for input elements #24

Closed dannyc closed 12 years ago

dannyc commented 12 years ago

Binding input values forces you to use a directives for every input element. For example:

directives = {
    'name@value': function(element) { return this.name;},
    'job@value': function(element) { return this.job; }
 };

Alternatively I was able to create a single directive for all input elements like so:

'input@value':   function(element) { return this[element.className];}

Which works but only if the input has a single class, otherwise returns undefined. Also since it catches all inputs if you have an input which you don't want bound that will automatically receive the undefined as well. Perhaps you can add to the parser that if it's an input element instead of changing the element's text, change the value?

Thanks.

pyykkis commented 12 years ago

Sounds good, will do.

To make things even simpler, Transparency could also match to the name attribute of the input element, e.g.,

<form>
  <input name="name" type="text" />
  <input name="job" type="text" />
</form>
$('form').render {name: "Foo", job: "Bar"}
<form>
  <input name="name" type="text" value="Foo" />
  <input name="job" type="text" value="Bar" />
</form>
dannyc commented 12 years ago

Great, I like the name idea since name is needed for inputs anyways. You think adding an "if" might hamper performance? I really have no idea. Also, I imagine this will be fixed along with it, but at moment when using the global Transparency.render() (did not test with the jQuery way) the binding does not work if the first parameter is a form element, but it does work for a div. For example <form id="edit"> <input name="name" class="name"> </form> Transparency.render(document.getElementById("edit"), data, directives);

Does not work, whereas <div id="edit"> <input name="name" class="name"> </div> Transparency.render(document.getElementById("edit"), data, directives); Does work.

pyykkis commented 12 years ago

Done! I also added support for textareas and options.

Please see https://github.com/leonidas/transparency/blob/master/spec/forms.spec.coffee for the details and review.

dannyc commented 12 years ago

Awesome! Thanks