Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a template in a backbone view that includes the parent element
    primarykey
    data
    text
    <p>I am attempting to refactor my backbone views to have all HTML related markup in external templates. Ideally I would like to have the element that the view is attached to in the external template as well. At the moment I have:</p> <p>The html template</p> <pre><code> &lt;h3 class="pull-left"&gt;Search&lt;/h3&gt; &lt;input id="customer-search-input" class="input-large search-query" placeholder="Cust #, Name, Suburb or Owner" /&gt; &lt;button id="customer-search-show-all" class="btn pull-right"&gt;Show All&lt;/button&gt; &lt;span id="update-time" class ="pull-right"&gt;&lt;/span&gt; &lt;table id="customer-search-results-table" class="tablesorter tablesorter-dropbox"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Customer Number&lt;/th&gt; &lt;th&gt;Customer Name&lt;/th&gt; &lt;th&gt;Suburb&lt;/th&gt; &lt;th&gt;Owner&lt;/th&gt; &lt;th&gt;Phone Number&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody id="customer-list-results"&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>And the backbone view that consumes the template:</p> <pre><code> define(['jquery','underscore', 'backbone', 'text!templates/customerSearch.html','text!templates/customerRow.html', 'jquery.tablesorter' ], function($, _, Backbone, customerSearchTemplate, customerRow) { // ... Other sub-views var CustomerSearch = Backbone.View.extend({ id:'customer-search', // would prefer to have these className: 'well', // in the template initialize: function(){ this.$el.html(customerSearchTemplate); this.customerSearchInput = this.$("#customer-search-input"); }, events: { "click #customer-search-show-all": "showAll", "keyup #customer-search-input": "search" }, search: function(){ var filteredCustomers = this.collection.search(this.customerSearchInput.val(), ['id','companyName','suburb','businessContact']); this.customerSearchResultsView = new CustomerSearchResultsView({collection: filteredCustomers}); this.customerSearchResultsView.render(); }, showAll: function() { this.customerSearchResultsView = new CustomerSearchResultsView({collection: this.collection}); this.customerSearchResultsView.render(); } }); return CustomerSearch; }); </code></pre> <p>Everything works but it would be great to be able to have the <code>id</code> and <code>className</code> as part of a wrapper div in the template. If I add this to the template then it appears correctly when rendered but is wrapped by another div by the backbone view. </p> <p>I'm trying to decouple everything as much as possible.</p> <p>Thanks!</p> <hr> <p><strong>Update 17 Oct 2012</strong></p> <p>Using the <code>view.setElement</code> method</p> <pre><code> var CustomerSearch = Backbone.View.extend({ template:_.template(customerSearchTemplate), initialize: function(){ this.setElement(this.template()); }, // ... }); </code></pre> <p>with template</p> <pre><code> &lt;div id="customer-search" class="well"&gt; &lt;h3 class="pull-left"&gt;Search&lt;/h3&gt; // ... &lt;/div&gt; </code></pre> <p>appears to work. Just wondering now if there is performance hit. Will report back. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload