sproutcore

sproutcore timer usage MaxInactiveInterval


Two absolute beginner questions. I have working code in my main.js enterState.

enterState: function(context) {
..
//keepAlive
var now = SC.DateTime.create(); 
if (now.get('hour') < 18){
    SC.info ("main_state:enterState:go %@", now.get('hour'));
    this.timer = SC.Timer.schedule({
         target: this,
         action: '_timerFired',
         interval: 5000, 
         repeats: YES
        });
    } else {
        SC.info ("MainState:enterState:nogo %@", now.get('hour'));
        };
..
    this.mainPane.append();
    },

_timerFired: function(){
        SC.info ("_timerFired %@", Date.now());
    },


exitState: function() {
        SC.info('main_state:exitState');
        this.timer.invalidate();
        this.mainPane.remove();             
    },

Question 1: the enterState is used every time a user goes to the main view, is the timer schedule initialized once or every time a user switches views?

Question 2: I think I need a query. e.q. the logged in username, to prevent an automatic logout due to the expired session MaxInactiveInterval. Is there sample code to get the spring username in the _timerFired function?

I saw the answer/solution of Maurits, thanks, but it is too complicated for me.


Solution

    1. This timer will be scheduled every time this state is being entered. If this is the root state of the application, that will be once. If this state is used to display the mainPane and you are leaving this state somehow, and returning, then this timer will be initialized every time you enter this state.

    2. You could keep the username as property of the current state (this._username) and as _timerFired is called with this being the current state (SC.Timer will take care of that through the target) you will have access to it. You'd need to set it somehow of course. Another solution is to read it directly from the controller you use for the login procedure.

    Nevertheless, this solution is prone to trouble. The main reason for this is that you are creating implicit states. I mean that being authenticated is an application state, and instead of making this explicit through the state chart, you hide it within one of the states. As I wrote in the comment: I came to the solution I posted in the other question because of trying many different solutions and hitting trouble. What I learned from those issues is that the statechart is your friend, and really trying to work with it will help you avoid loads of headaches!