14

During dev I would like to refresh my handlebar templates if they are saved live.

I already have a websocket channel that notifies me when a file saves. At that point I can force a reload of the particular template by updating a hash on the script tag src.

How can I notify all the Views that use this template that they need refreshing and force a refresh?

(How can I find them? How do I trigger a refresh?)

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • what about affecting a new templateName, like in http://stackoverflow.com/questions/9999064/ember-js-select-view-template-by-model-type-object-value/10006845#10006845. Could it work for you ? – sly7_7 Aug 17 '12 at 06:47
  • possibly, will give it a shot – Sam Saffron Aug 17 '12 at 07:04

2 Answers2

7

note this works for simple templates, but not for ones that are rendered into outlets

Getting this going was rather tricky:

var js = "template.js";
var templateName = "template";

Ember.TEMPLATES["empty"] = Handlebars.compile("")

// script loaders are the simplest way of getting a callback 
$LAB.script(js).wait(function(){
  $.each(Ember.View.views,function(){
     if(this.get('templateName')==templateName){
       this.set('templateName','empty');
       this.rerender();
       this.set('templateName',templateName);
       this.rerender();
     }
  });
})
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
2

In theory, you could do Ember.View.views.filterProperty('templateName', nameOfUpdatedTemplate).set('template', Ember.TEMPLATES[nameOfUpdatedTemplate]). That should force a re-render.

I have not tried this, and don't know what edge cases you might run into, but that would be the simplest approach I can think of.

Luke Melia
  • 8,389
  • 33
  • 41