0

In a Ruby on Rails 4 application I have a form which uses jQuery DatePicker (from the jquery-ui-rails Gem.

So I need to call

$('.datepicker').datepicker();

everytime my form is rendered.

I started with this (in my controller specific .js.coffee file.)

$ -> 
    $('.datepicker').datepicker

It went fine until I got the Turbolinks problem and had to change to:

ready = ->
    $('.datepicker').datepicker();

$(document).ready = ready
$(document).on 'page:change', ready

But I still have problems after hitting the submit button and there is one (or more) errors, so the form is re-rendered but none of the handlers (document.ready and document.on page:change) are called.

I've put this code in application.js

$(function(){
    console.log("application.js -> ready")
})

and I can see in the console that this is being called, but the functions defined in the controller specific .js file are not.

All the files seems to be loaded correctly (they have a <script> line in the <header> section; and appear in Chrome's Sources tab.

4

1 回答 1

1

我不清楚上面的语法,但从 javascript/jquery 的角度来看:

$(document).ready = ready

这不会注册一个函数来调用 ready 事件,它会破坏您应该调用以注册侦听器的 ready 函数。

于 2013-10-30T16:49:15.147 回答