How to correctly reload new version of module with steal?
I'm implementing live refresh of my application when some source file of the module changes, page is notified of the change. I want to reload module with steal and reinitilize the application. (it takes much less time then reloading/refreshing the whole page).
So when my application is nofied that module had been changed:
1) I delete the module from steal.Module.modules
2) steal new version of the module, so it gets added to modules but with another module.id
3) I search for depended modules (which have dependence with old moudle.id), and execute them, I also update dependencies module.id with new one.
3) then I reinitialize the application.
The code:
- var module = steal.Module.modules[moduleId]
- if (module){
- var id = module.id
- delete steal.Module.modules[moduleId]
- steal(data, function(){
- //remove the app
- $('my-app').remove()
- var modules = steal.Module.modules
- var newModule = modules[moduleId]
- for (var m in modules){
- // search for dependeces
- var dep = modules[m].dependencies.filter(function(dep){
- return dep ? dep.id == id: false
- })[0]
- if (dep){
- dep.id = newModule.id
- modules[m].executing = false
- modules[m].execute()
- }
- }
- setTimeout(function(){
- // init my app
- $('body').append(can.view.mustache('<my-app></my-app>')())
- }, 500)
- })
- }
It seems to work well.
The problems:
1) I'm not sure is there a way to reload module without deleting existing from steal.Module.modules and changing module.id
2) I have to use timeout to ensure that new version of module and dependencies where executed. I tried to use some deferreds (module.completed) but didn't manage to solve the issue.