Javascript 服务人员阻止backbonejs?
我已经用Backbone.js构建了一个web应用程序,它有很多对RESTful服务的调用,工作起来很有魅力 我尝试添加一个ServiceWorker来缓存以前的所有呼叫,以便它们可以脱机使用 我实际得到的是,我第一次打电话时,会出现以下错误: 未能加载资源:net::ERR\u失败 然而,在重新加载页面时,我得到了它的缓存数据 我的服务人员:Javascript 服务人员阻止backbonejs?,javascript,backbone.js,service-worker,Javascript,Backbone.js,Service Worker,我已经用Backbone.js构建了一个web应用程序,它有很多对RESTful服务的调用,工作起来很有魅力 我尝试添加一个ServiceWorker来缓存以前的所有呼叫,以便它们可以脱机使用 我实际得到的是,我第一次打电话时,会出现以下错误: 未能加载资源:net::ERR\u失败 然而,在重新加载页面时,我得到了它的缓存数据 我的服务人员: self.addEventListener('fetch', function(e) { // e.respondWidth Responds to t
self.addEventListener('fetch', function(e) {
// e.respondWidth Responds to the fetch event
e.respondWith(
// Check in cache for the request being made
caches.match(e.request)
.then(function(response) {
// If the request is in the cache
if ( response ) {
console.log("[ServiceWorker] Found in Cache", e.request.url, response);
// Return the cached version
return response;
}
// If the request is NOT in the cache, fetch and cache
var requestClone = e.request.clone();
fetch(requestClone)
.then(function(response) {
if ( !response ) {
console.log("[ServiceWorker] No response from fetch ")
return response;
}
var responseClone = response.clone();
// Open the cache
caches.open(cacheName).then(function(cache) {
// Put the fetched response in the cache
cache.put(e.request, responseClone);
console.log('[ServiceWorker] New Data Cached', e.request.url);
// Return the response
return response;
}); // end caches.open
console.log("Response is.. ?", response)
return response;
})
.catch(function(err) {
console.log('[ServiceWorker] Error Fetching & Caching New Data', err);
});
}) // end caches.match(e.request)
); // end e.respondWith
});
编辑:
我认为不需要任何Backbone.js web应用程序代码。
我使用Backbone.js模型和集合中的fetch方法。
像这样的电话
和
将在第一次重播时显示此错误。刷新页面后,我不需要请求即可获得此信息。全部来自缓存。
而我仍然没有执行的所有其他请求都将保留为错误。我看到的唯一问题是,当请求不在缓存中时,您将执行一次提取,但不会将该提取的结果返回给封闭的then处理程序。您需要添加报税表,以便:
return fetch(requestClone)
.then(function(response) {
否则,then处理程序中用于获取的return语句提供的任何数据都不会在链上传输
我还看到您没有返回caches.opencacheName提供的承诺。然后。。。。如果您想将在缓存中保存响应与在链上返回结果分离开来,这可能很好,但至少我会在这里添加一条注释,说明这正是我要做的,而不是让读者来判断是否意外丢失了return语句,或者是故意的。我在搜索了更多信息后解决了它。 Backbone.js我在Web应用程序中的视图用于:
this.listenTo(this.collection,"reset",this.render);
this.listenTo(this.collection,"add",this.addCollectionItem);
this.listenTo(this.collection,"error", this.errorRender);
而我的服务人员正在回复承诺
我必须将我的一些代码和Web应用程序视图更改为以下内容:
this.collection.fetch({},{reset:true})
.then(_.bind(this.render, this))
.fail(_.bind(this.errorRender,this))
或多或少…请包含一个。我编辑过:我认为不需要任何Backbone.js web应用程序代码。我使用Backbone.js模型和集合中的fetch方法。像和这样的调用将在第一次重播时显示此错误。刷新页面后,我不需要请求即可获得此信息。全部来自缓存。所有其他我仍然没有做的请求,都将保留错误