tags:

views:

14

answers:

2

I want to ensure data is saved in my sproutcore app before a user navigates away from the page. What is the best way to do this in Sproutcore?

A: 

I am assuming that by data you mean records. In that case you can set the 'commitRecordsAutomatically' property of MyApp.store to True.

Or if you can detect the user leaving your page, you can then call MyApp.store.commitRecords().

Suvir
+1  A: 

There's no specific way endorsed by Sproutcore. However I did something that looks roughly like this:

In core.js

MyApp = SC.Object.create({

  // ...

  storeIsDirty: function(){
    var statuses = this.store.statuses, storeKey;
    for(storeKey in statuses){
      if (statuses[storeKey] & SC.Record.DIRTY) return YES;
    }
    return NO;
  },

  storeIsBusy: function(){
    var statuses = this.store.statuses, storeKey;
    for(storeKey in statuses){
      if (statuses[storeKey] & SC.Record.BUSY) return YES;
    }
    return NO;
  }

});

Then in main.js

window.onbeforeunload = function(){
  var dirty = MyApp.storeIsDirty(),
    busy = MyApp.storeIsBusy();

  if (dirty || busy) {
    // User will be prompted with the return value
    return 'You have unsaved changes and will lose them if you continue.';
  }
}

I know that's overdue, but I hope that helps you or someone else.

If you have more questions, visit the IRC chat room at #sproutcore or check out the mailing list at [email protected].

Peter Wagenet