开发者

What is the proper way to detect window.unload in sproutcore

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?


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 sproutcore@googlegroups.com.


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜