How to update the template of a view in runtime?

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?

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:

More info on bound template names: Ember - 4.6 - Ember API Documentation

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

Here is the version you wanted, with dynamic templateName property on App.IndexView:

1 Like