backbone.js - Is it possible to pass cid field in template? -


i have view have template looks following:

<script type="text/template" id="template">    <div id="<%=cid=>"></div>    <label><%= label %></label>    <input type="text" id="search_input" />    <input type="button" id="search_button" value="search" /> </script> 

and need render template using model data, following:

render: function () {    var template = _.template( $("#template").html(), this.model.tojson());    this.$el.html( template );    return this; } 

but unfortunately this.model.tojson() doesn't pass cid (clientid) template. may explain how access cid in template , how handle thing?

tojson clones attributes array. that's why there's no cid. if need cid model, may mix in template object:

var templatedata = _.extend(this.model.tojson(), { cid: this.model.cid }); var template = _.template( $("#template").html(), templatedata); 

but if need unique id div - recommend use _.uniqueid()


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -