How to update the template of a view in runtime?


#1

First of all I’ll explain what I want to archieve because maybe I’m taking the wrong way.

I want to have a textarea where I write a Handlebars template and a div in the page where that template is rendered live, as I am writting it (unless it has syntax errors). Is for an interactive presentation.

My approach right now was to create a application and in its IndexView define a template: Ember.Handebars.compile("Hello {{name}}"). Outside the application, I listen for the keyup event on the textarea to get the new text live.

I tried to get the view instance and set('template', ...)

I also tried to reopen the App.IndexView and override the template.

Also tried both ideas and called rerender() on the view.

No effect, the view is never updated. Any idea?


#2

While I couldn’t tell you off the top of my head how to update the view’s template directly, there is the {{partial}} helper + bound template names you can get away with real cheap: http://emberjs.jsbin.com/gotihaya/1/edit

More info on bound template names: http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#toc_bound-template-names


#3

Sooooo, i could dynamically register templates as I type (naming them with a timestamp or any uniq identifier) and dynamically update the template name to that identifier.

Sounds possible. I’ll try tonight. Thanks


#4

Here is the version you wanted, with dynamic templateName property on App.IndexView: http://emberjs.jsbin.com/vibut/3/edit