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.
I'm not clear on the syntax above, but from a javascript/jquery perspective:
$(document).ready = ready
This doesn't register a function to call on the ready event, it clobbers the ready function that you are supposed to invoke to register a listener.