use Rails 4 turbolinks

This commit is contained in:
Lee Lawlor
2014-07-23 13:45:15 -04:00
parent 7a132477d0
commit 937d2eea47
25 changed files with 49 additions and 55 deletions

View File

@ -2,6 +2,7 @@
// This file is automatically included by javascript_include_tag :defaults
//= require jquery_ujs
//= require turbolinks
//= require ./jquery.cookie.js
//= require ./updateChart.js
//= require ./timeago.min.js

View File

@ -1,5 +1,5 @@
// when the dom is ready
$(document).ready(function() {
$(document).on('page:load ready', function() {
// if the cheerlights row exists
if ($('#cheerlights_row').length > 0) {

View File

@ -1,5 +1,5 @@
// when the document is ready
$(document).ready(function() {
$(document).on('page:load ready', function() {
// allow flash notices to be dismissed
if ($(".flash").length > 0) {

View File

@ -1,4 +1,4 @@
$(document).ready(function() {
$(document).on('page:load ready', function() {
// when a response is clicked
$('.response').click(function() {

View File

@ -21,21 +21,21 @@
*/
(function($){
// Change the values of this global object if your method parameter is different.
$.restSetup = { methodParam: '_method' };
// collects the csrf-param and csrf-token from meta tags
$(document).ready(function(){
$(document).on('page:load ready', function(){
$.extend($.restSetup, {
csrfParam: $('meta[name=csrf-param]').attr('content'),
csrfToken: $('meta[name=csrf-token]').attr('content')
});
});
// jQuery doesn't provide a better way of intercepting the ajax settings object
var _ajax = $.ajax, options;
function collect_options (url, data, success, error) {
options = { dataType: 'json' };
if (arguments.length === 1 && typeof arguments[0] !== "string") {
@ -62,7 +62,7 @@
});
}
}
function fill_url (url, data) {
var key, u, val;
for (key in data) {
@ -75,25 +75,25 @@
}
return url;
}
// public functions
function ajax (settings) {
settings.type = settings.type || "GET";
if (typeof settings.data !== "string")
if (settings.data != null) {
settings.data = $.param(settings.data);
}
settings.data = settings.data || "";
if ($.restSetup.csrf && !$.isEmptyObject($.restSetup.csrf))
if (!/^(get)$/i.test(settings.type))
if (!/(authenticity_token=)/i.test(settings.data)) {
settings.data += (settings.data ? "&" : "") + $.restSetup.csrfParam + '=' + $restSetup.csrfToken;
}
if (!/^(get|post)$/i.test(settings.type)) {
settings.data += (settings.data ? "&" : "") + $.restSetup.methodParam + '=' + settings.type.toLowerCase();
settings.type = "POST";
@ -101,31 +101,31 @@
return _ajax.call(this, settings);
}
function read () {
collect_options.apply(this, arguments);
$.extend(options, { type: 'GET' })
return $.ajax(options);
}
function create () {
collect_options.apply(this, arguments);
$.extend(options, { type: 'POST' });
return $.ajax(options);
}
function update () {
collect_options.apply(this, arguments);
$.extend(options, { type: 'PUT' });
return $.ajax(options);
}
function destroy () {
collect_options.apply(this, arguments);
$.extend(options, { type: 'DELETE' });
return $.ajax(options);
}
$.extend({
ajax: ajax,
read: read,
@ -133,5 +133,6 @@
update: update,
destroy: destroy
});
})(jQuery);
})(jQuery);

View File

@ -1,5 +1,5 @@
// execute on window load (and not document.ready), so that the sidebar is positioned correctly
$(window).load(function() {
$(window).on('page:load load', function() {
// if affix function exists
if ($.fn.affix) {