10

请注意,我认为此问题与 Backbone 或 JavaScript 无关,但有必要包含一些 Backbone 代码作为该问题的上下文。

相关代码

我有一个客户端骨干路由器,其路由采用名为contactId. 它看起来类似于:

Backbone.Router.extend({

  routes: {
    "jobs/new?contact_id=:contactId": "newForContact"
  },

  // Fetch the contact and initialize a new job model which 
  // is associated with that contact.
  newForContact: function(contactId) {
    var contact = new Contact(id: contactId);
    contact.fetch({
      success: _.bind(function(model, resp) {
        var job = new Job(contact: contact);
        this.new(job);
      }
    }, this));
  },

  // Show the JobView for the given job.
  new: function(jobModel) {
    view = new JobView(job: jobModel);
    $('body').append(view.render().el);
  }
};

现在我正在尝试在pushState打开的情况下使用此设置。

当我击中触发路线的newForContact路线时,一切都按预期工作。但是,如果此时我按下浏览器的后退按钮,我会contact.fetch()直接从浏览器的缓存中获得来自该方法的 JSON 响应。没有请求发送到服务器。

获取 JSON 响应

应用程序日志

您可以在 Rails 应用程序日志中看到这一点。在这一部分中,我访问了触发的路线newForContact

Started GET "/jobs/new?contact%5Bid%5D=1&contact%5Btype%5D=Customer" for 127.0.0.1 at 2012-10-31 22:41:48 +0000
Processing by JobsController#new as HTML
  Parameters: {"contact"=>{"id"=>"1", "type"=>"Customer"}}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Business Load (0.3ms)  SELECT "businesses".* FROM "businesses" WHERE "businesses"."id" IN (1)
  Rendered shared/_search_form.html.erb (0.3ms)
  Job Load (0.4ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."business_id" = 1 ORDER BY created_at desc
  Rendered jobs/_list.html.erb (1.5ms)
  Rendered jobs/index.html.erb within layouts/application (4.1ms)
  Rendered layouts/_head_content.html.erb (0.7ms)
  Rendered layouts/_flash.html.erb (0.0ms)
Cache read: views/jobs/main_nav/d6a805d9b6f285e424f207add4f35595
Read fragment views/jobs/main_nav/d6a805d9b6f285e424f207add4f35595 (0.4ms)
  Rendered layouts/_nav.html.erb (0.6ms)
  Rendered layouts/_header.html.erb (0.7ms)
Completed 200 OK in 12ms (Views: 8.0ms | ActiveRecord: 1.0ms)
Cache read: http://print.dev/customers/1?

您可以看到它此时使用 JSON 请求获取联系人。

Started GET "/customers/1" for 127.0.0.1 at 2012-10-31 22:41:48 +0000
Processing by CustomersController#show as JSON
  Parameters: {"id"=>"1"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Business Load (0.4ms)  SELECT "businesses".* FROM "businesses" WHERE "businesses"."id" IN (1)
  Customer Load (0.2ms)  SELECT "customers".* FROM "customers" WHERE "customers"."business_id" = 1 AND "customers"."id" = $1 LIMIT 1  [["id", "1"]]
  CustomerEmployee Load (0.3ms)  SELECT "customer_employees".* FROM "customer_employees" WHERE "customer_employees"."employer_id" IN (1)
  Job Load (0.4ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."contact_type" = 'Customer' AND "jobs"."contact_id" IN (1)
  Invoice Load (0.3ms)  SELECT "invoices".* FROM "invoices" WHERE "invoices"."client_id" IN (1)
  Job Load (0.5ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."contact_id" = 1 AND "jobs"."contact_type" = 'Customer' AND "jobs"."state" = 'finished' AND "jobs"."invoice_id" IS NULL
  Rendered customers/show.json.rabl (2.8ms)
Completed 200 OK in 67ms (Views: 3.2ms | ActiveRecord: 2.5ms)

此时,我会按下浏览器的后退按钮,但服务器上没有记录新请求。

Rails 环境

这只发生在我的登台服务器(Heroku)上,而不是在开发中。我可以通过在暂存环境中使用Pow运行应用程序来在本地重新创建它,该环境在 rails 配置中打开了缓存。

config.action_controller.perform_caching = true

请注意,即使打开了缓存,我也无法在开发环境中重新创建错误。

此问题发生在Chrome 22.0.1229.94FF 16.0.2Safari 6.0.1。我正在使用 Rails 3.2.8

可能相关的问题

看来这家伙和我有一个非常相似的问题。

现场样品

如果你真的想要,你可以在我的 Heroku 登台服务器上实时查看问题。

复制步骤(编辑:自从我修复了问题以来,这些不再起作用)。

  1. 使用电子邮件在此处登录:user@example.com 并通过:foobar
  2. 访问http://print-staging.herokuapp.com/customers/2。您应该会看到一个看起来很乱的对话框打开。
  3. 单击对话框中的“新作业”小链接。页面应该改变并且应该打开一个新对话框。
  4. 单击浏览器后退按钮。
4

2 回答 2

10

您可以在服务器端的响应中添加 no-cache 标头,这应该指示浏览器不缓存响应:

response.headers["Cache-Control"] = "no-cache"
于 2012-10-31T23:18:13.470 回答
2

我没有使用 Backbone,这正是我使用 AJAX / pushstate 所做的


history.pushState()在我的 AJAX 响应中使用。

var url = "http://foo.com/path/to/page";
var relative_url = UrlToRelative(url); // some function to convert a URL to relative
var absolute_url = UrlToAbsolute(url); // some function to convert a URL to absolute
history.pushState(
  {
    hash: "#"+relative_url,
    title: document.title,
    initialHref: absolute_url
  },
  document.title,
  url
);

我有一个事件监听器onPopState

window.onpopstate = function(event) {
  if (event.state && event.state.initialHref) {
    $.ajax({
      complete: null,
      data: {},
      dataType: "script",
      success: null,
      type: 'GET',
      url: event.state.initialHref
    });
  }
}

此事件侦听器发出 AJAX 请求。唯一的问题是,当返回到需要几秒钟才能获得服务器响应的页面时,屏幕需要几秒钟才能改变。并且没有微调器或忙碌进度指示器。

于 2012-11-06T18:09:34.410 回答