覆盖骨干网中的fetch方法

Ane*_*nth 7 javascript backbone.js

我想覆盖模型和集合中的fetch方法,以便在没有网络连接时从localstorage中获取数据.

这是集合中的fetch函数

fetch:function(){
   if(online){
      return Backbone.Collection.prototype.fetch.call(this) ;
   }else{
    // return Backbone.Collection.fetch event - with data from localstorage 
   }
}
Run Code Online (Sandbox Code Playgroud)

我在这里面临两个问题

a.成功或错误功能未执行

this.collection.fetch({
   success: function(data){ ... },
   error: function(){ ... }
});
Run Code Online (Sandbox Code Playgroud)

湾 如果没有连接,如何将数据设置为集合,以便我可以在成功函数中访问它

Ane*_*nth 3

经过研究,我找到了问题a的答案。

fetch:function(options){
    if(navigator.onLine){
        return Backbone.Collection.prototype.fetch.apply(this, options) ;
    }else{
     return //Trying to findout
    }
}
Run Code Online (Sandbox Code Playgroud)

将选项参数传递给 fetch 函数并将参数传递给 fetch.apply 后,我们将能够从调用函数访问 success 函数