0

I'm trying to detect when the user hits "refresh" from my app to load some data.

I have this:

var refresh = function() {
    alert('refresh');
};

var vm = {
    refresh: refresh,
    data: ko.observable()
};

However I never get the alert in my browser, and a breakpoint set at the opening of the function does not get hit when I refresh the page from this view. How can I properly use the refresh function?

SB2055
  • 12,272
  • 32
  • 97
  • 202
  • How does the view look like? Or are you talking about pressing F5? If so see http://stackoverflow.com/questions/3950029/handle-refresh-page-event-with-javascript – RainerAtSpirit Jun 13 '13 at 16:02
  • @RainerAtSpirit I'm just trying to click the refresh button in chrome to hit that alert. The view is just some standard html, nothing special there. I've tried placing this code in shell.js and a regular vm separately, still can't get it working – SB2055 Jun 13 '13 at 16:10
  • Best you can aim for is a `onbeforeunload` event, but at that point th e page is already reloading and would get fresh data anyway. http://stackoverflow.com/questions/4376483/javascript-how-to-overrite-page-reload-event. Maybe I'm missing something in the scenario? You might want to implement a huge refresh button instead that reloads the data. – RainerAtSpirit Jun 13 '13 at 16:51
  • @RainerAtSpirit - I was under the impression that 'refresh' is something I can define in any durandal vm, like viewAttached or create, that gets triggered when the page is refreshed. It seems like that's not the case - thanks for taking the time to make that clear to me – SB2055 Jun 13 '13 at 17:17

1 Answers1

1

I would suggest hooking into the canDeactivate method in your view model.

var refresh = function() {
    alert('refresh');
};

var canDeactivate = function(isClose){
    if (isClose)
    {
        refresh();
        return false;
    }
    else return true;
};

var vm = {
    data: ko.observable(),
    canDeactivate: canDeactivate
};
Jacques Snyman
  • 4,115
  • 1
  • 29
  • 49