简体   繁体   English

Backbone.js:如何将挂起的同步事件排队到服务器?

[英]Backbone.js: How to queue pending sync events to server?

I would like my Backbone.js based web app to function offline. 我希望我的基于Backbone.js的Web应用程序能够脱机运行。 Upon detecting offline state, how can I queue up Backbone's sync events so that they're sent to the server after connectivity is restored? 检测到脱机状态后,如何排队Backbone的同步事件,以便在连接恢复后将它们发送到服务器?

You can use two Backbone.sync methods and swap them out depending on offline/online state: 您可以使用两个Backbone.sync方法并根据离线/在线状态将它们交换出来:

// Detect sync however you want here
var state = getInternetConnectivityState();

// Save off a local copy of the default Backbone.sync
var ajaxSync = Backbone.sync;

// Create a new Backbone.sync function that works with local storage. I would suggest
// using store.js as it works x-browser, takes care of JSON serialization and is well  
// supported
var localSync = function(method, model, options) {
    if ('GET' === method) {
        model.set('name', store.get(model.id).name);
    }
    ...
}

Backbone.sync = function() {
    if ('offline' === getInternetConnectivityState()) {
        localSync.call(this, arguments);
    }
    else {
        ajaxSync.call(this, arguments);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM