Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would one refresh templates in ember (live)?

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?)

like image 203
Sam Saffron Avatar asked Aug 17 '12 01:08

Sam Saffron


2 Answers

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();
     }
  });
})
like image 176
Sam Saffron Avatar answered Nov 08 '22 21:11

Sam Saffron


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.

like image 2
Luke Melia Avatar answered Nov 08 '22 21:11

Luke Melia