4

我希望我的基于 Backbone.js 的网络应用程序能够离线运行。检测到离线状态后,如何将 Backbone 的同步事件排队,以便在连接恢复后将它们发送到服务器?

4

1 回答 1

1

您可以使用两个 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);
    }
}
于 2013-09-24T20:34:54.843 回答