2 $(document).on('click', '.toggle-persist button', function() {
3 var toggle_group = $(this).parents('[data-remote-href]').first();
4 var want_persist = !toggle_group.find('button').hasClass('active');
5 var want_state = want_persist ? 'persistent' : 'cache';
6 toggle_group.find('button').
7 toggleClass('active', want_persist).
8 html(want_persist ? 'Persistent' : 'Cache');
9 $.ajax(toggle_group.attr('data-remote-href'),
16 toggle_group: toggle_group,
17 want_state: want_state,
21 done(function(data, status, jqxhr) {
23 // Remove "danger" status in case a previous action failed
24 $('.btn-danger', context.toggle_group).
26 removeClass('btn-danger');
27 // Update last-saved-state
29 attr('data-persistent-state', context.want_state);
31 fail(function(jqxhr, status, error) {
34 // Add a visual indication that something failed
36 addClass('btn-danger').
37 removeClass('btn-info');
38 // Change to the last-saved-state
39 saved_state = context.toggle_group.attr('data-persistent-state');
41 toggleClass('active', saved_state == 'persistent').
42 html(saved_state == 'persistent' ? 'Persistent' : 'Cache');
44 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
45 // Request cancelled due to page reload.
46 // Displaying an alert would be rather annoying.
47 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
48 window.alert("Request failed: " +
49 jqxhr.responseJSON.errors.join("; "));
51 window.alert("Request failed.");