【问题标题】:How do I trigger & configure Bootstrap Tour based on the actions the user is on?如何根据用户的操作触发和配置 Bootstrap Tour?
【发布时间】:2016-11-21 07:11:34
【问题描述】:

我正在使用Bootstrap Tour,它的设置非常简单。

这些是说明:

// Instance the tour
var tour = new Tour({
  steps: [
  {
    element: "#my-element",
    title: "Title of my step",
    content: "Content of my step"
  },
  {
    element: "#my-other-element",
    title: "Title of my step",
    content: "Content of my step"
  }
]});

// Initialize the tour
tour.init();

// Start the tour
tour.start();

就我而言,我有一个Profiles#IndexProfiles#ShowDashboard#Index——所有这些都有不同的元素,我想介绍一下。所以我需要为所有不同的动作/视图指定不同的元素。

我也只想在以下情况下触发游览:

  • 用户第一次登录(可以通过current_user.sign_in_count < 2判断)。
  • 仅在用户首次登录时,而不是在他们刷新页面时。

我正在使用 Devise,仅供参考。

我现在将默认 JS 放在我的 app/assets/javascripts/profiles.js 中。我应该把它移到其他地方才能达到我的上述条件吗?

编辑 1

实际上,我以为它会正常工作,但我什至无法让它工作。

我在我的Profiles#Show 页面上,但没有触发游览。它是这样设置的:

这是我的application.html.erb

<%= stylesheet_link_tag "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tour/0.11.0/css/bootstrap-tour.min.css", 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tour/0.11.0/js/bootstrap-tour.min.js", 'application', 'data-turbolinks-track': 'reload' %>

这是我的application.js

$(document).on('turbolinks:load', function() {
  var tour = new Tour({
    backdrop: true,
    steps: [
    {
      element: "#ratings-controls",
      title: "Ratings Controls",
      content: "Here you can rate this recruit so you can easily remember how they performed."
    },
    {
      element: "div.action-buttons a#favorite-btn",
      title: "Favorite",
      content: "Here you can easily favorite a player"
    },
    {
      element: "a.my-favorites",
      title: "My Favorites",
      content: "After you favorite a player, they automagically get added to your list of favorites -- which you can easily view here."
    }
  ]});

  // Initialize the tour
  tour.init();

  // Start the tour
  tour.start();
});

我最初在我的 app/assets/javascripts/profiles.js 中有这个,但当它停止工作时,我将它移到 application.js 只是为了确保没有其他东西覆盖它。

在我的Profiles#Show 中,我肯定拥有所有这 3 个元素,您可以在下面呈现的 HTML 中看到:

<div id="ratings-controls" class="profile-data">
  <table class="table table-condensed">
      <tbody>
      <tr>
          <td>
              <p>
                <button type="button" class="btn btn-success m-r-sm slider-step-value" id="slider-step-value-speed-wacky-dip-st-george-s-college-bcdda202-c498-4baa-867d-200662fc785b" data-speed-value="0">0</button>
                Speed
              </p>
              <div id="slider-speed" class="slider"></div>
          </td>
          <td>
              <p>
                <button type="button" class="btn btn-info m-r-sm slider-step-value" id="slider-step-value-tackling-wacky-dip-st-george-s-college-bcdda202-c498-4baa-867d-200662fc785b" data-tackling-value="0">0</button>
                Tackling
              </p>
              <div id="slider-tackling" class="slider"></div>
          </td>
      </tr>
      <tr>
          <td>
              <p>
                <button type="button" class="btn btn-primary m-r-sm slider-step-value" id="slider-step-value-dribbling-wacky-dip-st-george-s-college-bcdda202-c498-4baa-867d-200662fc785b" data-dribbling-value="0">0</button>
                Dribbling
              </p>
              <div id="slider-dribbling" class="slider"></div>
          </td>
          <td>
              <p>
                <button type="button" class="btn btn-warning m-r-sm slider-step-value" id="slider-step-value-passing-wacky-dip-st-george-s-college-bcdda202-c498-4baa-867d-200662fc785b" data-passing-value="0">0</button>
                Passing
              </p>
              <div id="slider-passing" class="slider"></div>
          </td>
      </tr>
      </tbody>
  </table>
</div>

<div class="action-buttons">
  <a class="btn btn-xs btn-success" id="favorite-btn-29" data-remote="true" rel="nofollow" data-method="post" href="/profiles/wacky-dip-st-george-s-college-bcdda202-c498-4baa-867d-200662fc785b/favorite"><i class='fa fa-thumbs-up'></i> Favorite</a>
</div>

<a class="my-favorites" href="/profiles?filter=favorites">
  <i class="fa fa-list"></i> My Favorites
</a>

此外,Bootstrap Tour CSS 和 JS 也包含在我呈现的 HTML 中。

所以第一个问题是......我什至如何让它工作?然后我可以去找其他人。

编辑 2

我在application.js 中添加了一些调试语句,结果如下。

这就是我的application.js 现在的样子:

$(document).on('turbolinks:load', function() {
  console.log('Turblinks Has Loaded -- This is Before Tour is Initialized.')
  var tour = new Tour({
    backdrop: true,
    steps: [
    {
      element: "#ratings-controls",
      title: "Ratings Controls",
      content: "Here you can rate this recruit so you can easily remember how they performed."
    },
    {
      element: "div.action-buttons a#favorite-btn",
      title: "Favorite",
      content: "Here you can easily favorite a player"
    },
    {
      element: "a.my-favorites",
      title: "My Favorites",
      content: "After you favorite a player, they automagically get added to your list of favorites -- which you can easily view here."
    }
  ]});

  // Initialize the tour
  tour.init();
  console.log('This is after Tour has been initialized.')

  // Start the tour
  tour.start();
  console.log('This is after Tour has been started.')
});


Turblinks Has Loaded -- This is Before Tour is Initialized.
bootstrap-tour.min.js:22 Uncaught TypeError: Cannot read property 'extend' of undefined(…)

这可能是什么原因造成的,我该如何进一步调试它?错误似乎在bootstrap-tour.min.js 文件中。

【问题讨论】:

  • 您有什么问题?我看到您已经拥有全部或大部分代码。您尝试过什么或遇到过什么问题?
  • @marczking 我的问题是它根本不起作用。我有所有的代码,但是当页面完成加载时,Tour 并没有像你期望的那样执行。我错过了什么?或者我可以尝试什么?我在 JS 控制台中没有看到任何错误。
  • 你能用基本结构做一个小提琴或codepen吗?我的猜测是,这不是直接的 rails 问题。当然,您需要正确填充视图,但我认为您应该能够仅使用 HTML 和 JavaScript 将其精简为基础。今天晚些时候我也会试着看看。但如果你有一支笔左右,请随意链接它;)
  • 什么是 turbolinks,它有加载事件吗?也许您可以尝试在文档加载后开始游览?
  • 我可以看到这种破坏的一种方法是如果您使用jquery-turbolinks gem。您是否在 turbolinks:load 块中 console.log('something') 以确保其行为符合预期?

标签: ruby-on-rails twitter-bootstrap asset-pipeline ruby-on-rails-5 bootstrap-tour


【解决方案1】:

新答案

在阅读Bootstrap Tour docs 之后,听起来您的问题可能是 Bootstrap 游览在本地存储中保存 cookie 以跟踪用户游览进度的方式。换句话说,游览无法开始的原因是因为 Bootstrap 游览在您的浏览器本地存储中查找并认为您已经完成了。因此,要解决此问题,您有三个选择,具体取决于您的具体情况:

  1. 强制引导导览不使用存储

    // Instance the tour
    var tour = new Tour({
      storage: false,
      steps: [ ...]
    ]});
    

  1. 强制开始游览

    // Start the tour
    tour.start(true);
    

  1. 强制重新开始游览

    // Restart the tour
    tour.restart();
    

原答案

我正在根据我上面提供的 cmets 和一些调试做出一些假设,看起来您的 javascript 文件未包含在正确的顺序或方式中。最近 Rails 5 对 turbolink 的更改使这更加复杂。在 Rails 的快速原型设计世界中,这是我转向轻量级宝石以进行资产包含的时候。在你的情况下,我会推荐:

摆脱:

application.html.erb

<%= stylesheet_link_tag "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tour/0.11.0/css/bootstrap-tour.min.css", 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tour/0.11.0/js/bootstrap-tour.min.js", 'application', 'data-turbolinks-track': 'reload' %>

并像这样安装bootstrap-tour-rails gem

宝石文件

gem 'bootstrap-tour-rails'

application.js

//= require bootstrap-tour

application.css

*= require bootstrap-tour

【讨论】:

  • 我试过这个,但问题是application.css 要求。我只有application.scss,gem 没有sass 支持。
  • 所以我尝试了您的解决方案,以确保我的依赖项以正确的顺序加载并解决了该问题。然而,巡演还没有开始。所以页面加载,我看到了我所有的 console.log 消息....但我没有看到游览实际开始。
  • @marcamillion 关于 gem,这是一个解决方案:如果您的 application.scss 中有任何 sass,请将其从该文件中移出并放入您的 assets/stylesheets 文件夹中名为 custom.css.scss 的文件中(只要application.scss 中有*= require tree .custom.css.scss 中的样式将在每个视图中可用),将application.scss 重命名为application.css(现在你没有任何sass)。跨度>
  • @marcamillion 关于依赖顺序...所有console.logs 运行,游览没有开始,并且没有错误? ...这听起来像是您在var tour = ... 中的代码有问题。我会稍微看一下文档,看看我能不能发现它
  • 看来你是对的。这个问题似乎与存储有关。您指定storage: false 的修复对我来说是完美的修复。我不需要指定tour.start(true)。非常感谢。
【解决方案2】:

所以我没有看到你包括bootstrap.js,所以你可能会错过。在使用 bootstrap-tour.js 之前包含它,因为这取决于 bootstrap.js。查看overview and docs

我做了一个非常基本的笔,总是遇到这个问题:$element.popover not defined。包含 bootstrap.js 后,游览很好地初始化(没有样式,但可以工作)。这是我的未设置样式,但可以工作 pen(基于您提供的代码)。

编辑

所以它在我的pen 上工作。但是,一旦您单击“结束游览”并重新加载页面,游览将不会重新开始。所以它正在设置一个cookie,因此不会重新开始游览。删除 cookie,游览将再次启动。

所以我认为这可能一直是您的问题。游览应该可以正常工作,但一旦您结束游览,它就不会重新开始。

这是因为一切看起来都正确,甚至是您的代码本身!

【讨论】:

  • 是的,我已经包含了 Bootstrap。我在项目的其余部分使用它。你对问题的原因是正确的,但修复是模糊的。因此,我支持你....但不能接受它作为答案,因为它没有具体告诉我修复是什么。感谢您帮助解决这个问题!
  • 谢谢,很高兴它对你有所帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多