除非刷新,否则日期选择器不会在Rails中显示 [英] date picker does not show in Rails unless refresh

查看:53
本文介绍了除非刷新,否则日期选择器不会在Rails中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将日期选择器放在Rails(v4.1.8)中的表单中。我使用eyecon datepicker(禁用过去的日期)。 http://www.eyecon.ro/bootstrap-datepicker/

I put the date picker in a form in Rails (v4.1.8). And I use eyecon datepicker (disabling dates in the past). http://www.eyecon.ro/bootstrap-datepicker/

  <div class="form-group">
    <label for="expiration" class="col-sm-2 control-label">
      Expiration
    </label>
    <div class="col-sm-10">
      <input type="text" class="span2 form-control" name="expiration" id="dpd1" data-date-format="yyyy-mm-dd">
    </div>
  </div>

如果我被定向到此页面,则单击日期表单时不会显示日期选择器。但是,如果我通过chrome中的control + r刷新页面。日期选择器效果很好。
或者,如果我手动在网址栏中输入地址并按Enter,日期选择器也可以使用。

If I was directed to this page, the date picker does not show up when I click the date form. However, if I fresh the page by control+r in chrome. The date picker works well. Alternatively, if I enter the address in the url bar by hand and hit enter, the date picker also works.

这太奇怪了。有人可以向我解释吗?

It's so strange. Can someone explain it to me?

另一个事实可能会有所帮助:如果我将代码另存为html而不是使用Rails。

Another fact may help: If I save the code as html instead of using Rails. It works fine.

谢谢

推荐答案

在那里经历了数百万次-这是因为涡轮链接 turbolinks gem可提高您的网站速度-单击任何导航链接时,它不会重新加载整个页面,而仅会更改其中的那些部分(极大简化了它的作用,请阅读此处了解更多详细信息)

Been there millions of time - it is because of turbolinks. turbolinks gem makes your website much faster - it does not reload the whole page when you click any navigation links, but only those parts of it which are to change (it's huge simplification of what it does, read here for more details)

自然地,您的javascript不会被Turbolink跟踪,因此在加载新内容时它不会执行,从而使所有元素都没有通常没有Turbolinks的钩子或功能。当您刷新页面,加载整个页面并执行所有javascript以便一切都能按预期运行时。

Naturally your javascript is not tracked by turbolinks and hence it is not executed when new content is loaded, leaving all your elements without any hooks or functionality you would normally have without turbolinks. WHen you refresh the page, the whole page is loaded and all javascript is executed so all works as expected.

我通常通过定义一个辅助方法来处理它:

I usually deal with it by defining a helper method:

window.onInit = function(handler) {
  $(document).on('ready page:change', handler)
}

然后包装要在其中每个页面运行的所有JavaScript :

And then wrap all your javascript which is to run for every page in it:

onInit(function() {
  $('#dpd1').datepicker();
}):

这篇关于除非刷新,否则日期选择器不会在Rails中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆