Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to most easily render both i18n text AND model attributes (using Handlebars template) in Backbone.Marionette View
    primarykey
    data
    text
    <p>Rendering templates is a piece of cake when the data displayed within the template is either internationalized text OR model attributes, but when it comes to rendering BOTH within one template, I can't seem to find a clean solution.</p> <p>For reference, I'm utilizing i18n via Require.js's i18n plugin.</p> <p>Let's assume I have a simple template:</p> <pre><code>&lt;h3&gt;{{displayText.load}} #{{id}}&lt;/h3&gt; &lt;h4 id="loading-load-data"&gt;{{displayText.loadingLoadData}}...&lt;/h4&gt; </code></pre> <p>The <code>displayText</code> object represents i18n text, while the <code>id</code> item represents a Backbone Model attribute.</p> <p>Using Backbone's <code>template</code> property on a View, I can do the following in order to render a template with i18n text, but without Model attribute data:</p> <pre><code>return Backbone.Marionette.ItemView.extend({ template: function () { var compiledTemplate = Handlebars.compileClean(template); // localizedText represents the i18n localization object, using the Require.js i18n plugin return compiledTemplate(localizedText); }, // some more View properties and methods }); </code></pre> <p>However, once I want to also display the Model data, this no longer works, primarily due to <code>this</code> being undefined within the template attribute (so I can't reference <code>this.model.attributes</code>), and it seems that I have to fall back to overriding the <code>render()</code> method, passing both the i18n object AND Model attributes to the template, as such:</p> <pre><code>return Backbone.Marionette.ItemView.extend({ template: Handlebars.compileClean(template), render: function() { var templateParams = _.extend({}, this.model.attributes, localizedText), renderedTemplate = this.template(templateParams); this.$el.html(renderedTemplate); this.bindUIElements(); this.delegateEvents(); return this; } }); </code></pre> <p>I'd really love to leave Marionette's default handling of <code>render()</code> in place, and solely use the template property to render both i18n text AND the Model data. Is this possible?</p> <p><strong>BONUS</strong>: Assuming I DO have to override <code>render()</code>, I'm noticing that while doing so, the <code>this.ui</code> attribute, provided on Marionette Views, no longer wraps each item as a jQuery object. This means that:</p> <pre><code>this.ui.loadingNotification.show(); </code></pre> <p>stops functioning, throwing an <code>Uncaught TypeError: Object #loading-load-data has no method 'show'</code>. Why is this, and how can I restore proper <code>this.ui</code> jQuery-wrapping functionality?</p> <p><strong>EDIT</strong>: Solved the <strong>BONUS</strong>; simply have to toss in a <code>this.bindUIElements()</code> call within the <code>render()</code> method to properly bind the elements to the <code>ui</code> hash. See the <code>render()</code> example above.</p>
    singulars
    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.
    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