ember.js - Call custom function when view is rendered, from one place and not repeating code for every view -
in ember application want phone call custom function modifications of dom elements, solution found repeat below code many times many views/routes have
for example
rendering indexview
indexview = ember.view.extend({ didinsertelement:function(){ //my custom function phone call goes here.. myfunction(); } }); rendering otherview
otherview = ember.view.extend({ didinsertelement:function(){ //my custom function phone call goes here.. myfunction(); } }); rendering moreview
moreview = ember.view.extend({ didinsertelement:function(){ //my custom function phone call goes here.. myfunction(); } }); is there way of calling myfunction globaly whenever view rendered? not want repeat code every single view render.
thanks!
there 2 ways handle this. create base of operations class, or reopen global view class , insert code. prefer first, since it's easier maintain , track downwards issues.
reopen view class , tack on additional method triggers on didinsertelement
ember.view.reopen({ dostuff:function(){ //myfunction(); }.on('didinsertelement') }); in case of debouncing
ember.view.reopen({ dostuff:function(){ ember.run.debounce(this.dorealstuff, 200); }.on('didinsertelement'), dorealstuff: function(){ console.log('foo'); } }); http://emberjs.jsbin.com/gecoziwe/1/edit
create base of operations class , extend (my preferred method, since it's easier track downwards issues, can't enough)
app.baseview = ember.view.extend({ dostuff:function(){ //myfunction(); }.on('didinsertelement') }); app.fooview = app.baseview.extend({ }); app.barview = app.baseview.extend({ }); ember.js
No comments:
Post a Comment