javascript - Get DOM element from template in backbone view -


i using backbone.js display list of people , data.

every person has it's own <div>. div generated _.template , contains <input> fields display person's data can adjusted.

there button class=".save". in view have function bound click of button. looking best approach values of <input>-tags in div belonging model.

here approach i'm wondering if there's better one. in template have dyanmically assigned id's dom elements based on id of model. use same logic find element in view.

template

<input value="<%=name%>" id="name_<%=id%>"/> <input value="<%=age%>" id="age_<%=id%>"/> <input value="<%=address%>" id="address_<%=id%>"/> <button class=".save">save</button> 

view

events:{     "click .save":"saveperson" },  saveperson: function(){     this.model.set({         name: $("#name" + this.model.id).val(),         address: $("#address_" + this.model.id).val(),         age: $("#age_" + this.model.id).val()     });     this.model.save(); } 

if every person different view instance own template, can define scope of selector view's template:

template

<form id="<%-id%>">   <input value="<%-name%>" name="name"/>   <input value="<%-age%>" name="age"/>   <input value="<%-address%>" name="address"/>   <button class=".save">save</button> </form> 

view

events:{     "click .save":"saveperson" },  saveperson: function(){     this.model.set({         name: this.$("input[name='name']").val(),         age: this.$("input[name='age']").val(),         address: this.$("input[name='address']").val()     });     this.model.save(); } 

otherwise, if have many persons in same template/view instance (not nice), catch current person's id var personid = this.$("#"+this.model.id), use personid inside selectors above.

ps: use <%-value%> instead of <%=value%> interpolate value, , have html-escaped.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -