后退按钮不会重新触发骨干应用程序

时间:2014-10-17 20:04:38

标签: javascript jquery ruby-on-rails backbone.js marionette

我在Backbone Marionette应用程序中遇到了一个奇怪的场景。如果用户点击了facebook / twitter链接,然后点击浏览器后退按钮,而不是重新加载我的应用程序,则只显示json,就像我转到example.com/posts.json而不是{{1} }。如果用户手动重新加载页面,它可以很好地加载,但是用户看到json会很困惑。

如果我在example.com/posts上,然后手动导航说出example.com/posts并点击后退按钮就可以了。似乎是在点击我的应用程序中的链接时出现了这种情况。

任何想法我都能"强迫"当单击后退按钮导航回我的应用程序时,Backbone应用程序重新加载?

编辑:

添加我的Rails控制器 - 它正在点击索引。但是当按下后退按钮时,它似乎会触发json与html(虽然在同一页面上刷新会导致它击中正确的html):

gmail.com

index.html.erb视图:

class SurveysController < ApplicationController    
  # GET /surveys
  # GET /surveys.json
  def index
    if cookies[:sample_user_id] && !User.find(cookies[:sample_user_id]).admin?
      if !cookies[:sample_appuser_token] 
        appuser = Appuser.create
        sign_in_appuser appuser
      else
        appuser = current_appuser
      end
      @surveys = User.find(cookies[:sample_user_id]).surveys.includes([:responses => [:choices], :questions => [:answers]])
    else
      if !cookies[:sample_appuser_token] 
        appuser = Appuser.create
        sign_in_appuser appuser
      else
        appuser = current_appuser
      end
      @surveys = Survey.where(status: "Submitted")
      if cookies[:sample_user_id]
        User.find(cookies[:sample_user_id]).surveys.each do |survey|
          @surveys << survey
        end
      end
    end
  end

SurveysRouter:

<div id="wrap">
  <div id="header-region"></div>
  <div id="main-region"></div>
  <div id="push"></div>
</div>
<div id="footer-region"></div>
<div id="dialog-region"></div>
<%= debug(params) if Rails.env.development? %>

<%= javascript_tag do %>
  $(function() {
    Sample.start({
      environment: "<%= Rails.env %>"
    });
  });
<% end %>

列表控制器:

@Sample.module "SurveysApp", (SurveysApp, App, Backbone, Marionette, $, _) ->

  class SurveysApp.Router extends Marionette.AppRouter
    appRoutes:
      ""            : "list"
      ":id"         : "show"
      ":id/take": "take"

  API =
    list: ->
      if $.cookie('sample_user_email') != null
        new SurveysApp.List.Controller
      else
        window.location = "/signin"

    show: (id) ->
      window.fragment = Backbone.history.fragment
      if $.cookie('sample_user_email') != null
        new SurveysApp.Show.Controller
          id: id
      else
        console.log Backbone.history.fragment
        window.location = "/signin" + "#" + window.fragment

    take: (id, survey) ->
      new SurveysApp.Take.Controller 
        id: id 
        survey: survey

    newSurvey: (surveys, survey) ->
      if $.cookie('sample_user_email') != null
        new SurveysApp.New.Controller 
          surveys: surveys
          survey: survey
      else
        window.location = "/signin"

  App.vent.on "survey:clicked", (survey) ->
    App.navigate "/" + survey.id
    API.show survey.id

  App.vent.on "new:survey", (surveys, survey) ->
    API.newSurvey surveys, survey

  App.vent.on "take:survey:button:clicked", (survey) ->
    App.navigate "/" + survey.id + "/take"
    API.take survey.id, survey

  App.vent.on "survey:list", ->
    App.navigate "/"
    API.list()

  App.addInitializer ->
    new SurveysApp.Router
      controller: API

链接所在的页脚模板:

@Sample.module "SurveysApp.List", (List, App, Backbone, Marionette, $, _) ->

  class List.Controller extends App.Controllers.Application

    initialize: ->
        surveys = App.request "survey:entities"

        App.execute "when:fetched", surveys, =>
            @layout = @getLayoutView()

            @listenTo @layout, "show", =>
          @surveyRegion surveys
          @bannerRegion surveys

        @show @layout
        App.request "header:footer"

    surveyRegion: (surveys) ->
      surveyView = @getSurveyView surveys

      @listenTo surveyView, "childview:survey:clicked", (args) ->
        if args.model.get('status') == "Draft"
          App.vent.trigger "new:survey", surveys, args.model
        else
            App.vent.trigger "survey:clicked", args.model


      @listenTo surveyView, "childview:take:survey:button:clicked", (child, args) ->
        survey = args.model 
        survey.set(preview: true)
        App.vent.trigger "take:survey:button:clicked", survey

      @show surveyView, region: @layout.surveyRegion

    getLayoutView: ->
      new List.Layout

    getSurveyView: (surveys) ->
      new List.Surveys
        collection: surveys

    bannerRegion: (surveys) ->
      bannerView = @getBannerView surveys

      @listenTo bannerView, "new:survey:clicked", ->
        App.vent.trigger "new:survey", surveys

      @show bannerView, region: @layout.bannerRegion

    getBannerView: (surveys) ->
      new List.Banner
        collection: surveys

1 个答案:

答案 0 :(得分:1)

您所描述的内容使得example.com/posts.json的声音被添加为浏览器历史记录的条目。但是,AJAX调用通常不会添加到历史记录中,因此您的代码中似乎必须包含错误的router.navigatewindow.location =行或类似内容。但是,如果没有看到相关代码,就无法确定。