javascript - SetTimeout inside emberjs model -
i have in emberjs application model doesn't has database data. have create model because want object pools requests server not binded controller or view.
something like: statuschecker
inside application.create this:
webapp = ember.application.create({ rootelement: '#content', log_transitions: true, ready: function() { return webapp.status_helper = webapp.statushelper.create({ timer: 15 }); } });
this instantiate webapp.statushelper
object must unique application. when call init
on object start polling using default timer:
webapp.statushelper = ember.object.extend({ init: function(params) { this.timer = this.get("timer"); this.timeoutid = null; this.controller = null; this.start(); }, start: function() { this.execute(); }, stop: function() { cleartimeout(this.timeoutid); }, settimer: function(newtime) { this.stop(); this.timer = newtime; this.start(); }, execute: function() { var $this = this; this.stop(); $.get("/status").done(function(data) { console.log(data); if (data["status"] === 0) { // continue trying $this.timeoutid = settimeout(function() { $this.execute(); }, $this.timer * 1000); } else if (data["status"] === 1) { // go given controller $this.settimer(15); $this.transitiontoroute('index'); // doesn't work } else { $this.settimer(15); $this.transitiontoroute('problem'); // doesn't work } }); } });
the first problem cant change routes model using this: $this.transitiontoroute('problem');
. there way model?
the second problem when controller need specify interval statushelper
right after need stop timer , start interval, this:
webapp.indexroute = ember.route.extend setupcontroller: (controller) -> webapp.status_helper.settimer 15 webapp.problemroute = ember.route.extend setupcontroller: (controller) -> webapp.status_helper.settimer 1
this happens because need check if problem solved every second, , index page transitions problem page whenever needs. after transition 2, , more, intervals working on same job.
how can stop , make 1 function work?
p.s.: if find need change model else can change it.
tl;dr
- how can change routes inside controller?
- how can have 1 function running interval?
- is there (better) way?
edit
version of libraries:
debug: ember.version : 1.0.0-rc.1 ember.js:348 debug: handlebars.version : 1.0.0-rc.3 ember.js:348 debug: jquery.version : 1.9.1
the first problem cant change routes model using this: $this.transitiontoroute('problem');. there way model?
you can't/shouldn't access router model. whenever find necessary it's clue maybe code writing should not model. in case webapp.statushelper
should controller. like:
webapp.statuscontroller = ember.controller.extend({ //your code here });
then can rid of init() app.ready() hook , instead use setupcontroller hook of applicationroute configure , start timer. , of course since you're in controller able access router.
how can stop , make 1 function work?
so need start/stop timer when transitioning new routes. use route's activate
, deactivate
, , setupcontroller
hooks turn timer on/off when routes change. way can ensure timer running when want to.
webapp.indexroute = ember.route.extend activate: (controller) -> this.controllerfor('status').settimer 15 this.controllerfor('status').start() deactivate: (controller) -> this.controllerfor('status').stop() webapp.indexroute = ember.route.extend activate: (controller) -> this.controllerfor('status').settimer 1 this.controllerfor('status').start() deactivate: (controller) -> this.controllerfor('status').stop()
Comments
Post a Comment