简体   繁体   中英

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

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();
     }
  });
})

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM