At first, all the javascript bounded are running pretty well.
However, from the moment I navigate to another page, any event is being fired multiple times. If I refresh the page, everything is back to normal.
In the jquery.turbolink docs, there is an alert about binding the document events inside a $(function()) block. However, seems like the coffescript works like this by default. So, what should I do?
This is my environment:
Gemfile:
gem 'turbolinks'
gem 'jquery-turbolinks'
application.js
//= require jquery
//= require jquery.turbolinks
//..app js
//= require turbolinks
application.html.erb
<html>
<head>
<meta charset="utf-8">
<title><%= t 'brand' %> </title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= yield :head %>
<%= csrf_meta_tags %>
<meta name="description" content="<%= yield :page_description %>">
<meta name="keywords" content="<%= yield :page_keywords %>">
</head>
controller.js.coffee
$(document).ready ->
$(document).on 'click', '.addition .label i', (e) ->
console.log ".addition .label i 'click' fired!"
The thing is, apparently the javascript file generated by coffeescript IS inside a block by default. Check it out:
controller.js (generated by coffeescript)
function() {
$(document).ready(function() {
$(document).on('click', '.addition .label i', function(e) {
console.log(".addition .label i 'click' fired!");
});
...
So, what should I do to use coffeescript + JQuery Turbolinks properly?
Should I make a different configuration?
Cheers.