Skip to content

Commit

Permalink
JS: Add actions.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lee committed Oct 7, 2016
1 parent 1d467ee commit d003c3a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions r2/r2/public/static/js/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
!function(r) {
r.actions = {
trigger: function(actionName, payload) {
payload = payload || {};
payload.action = actionName;
var eventName = 'action:' + actionName;
var $e = $.Event(eventName, payload);

$(document.body).trigger($e);

if ($e.isDefaultPrevented()) {
$(document.body).trigger($.Event(eventName + ':failure', payload));
} else {
$(document.body).trigger($.Event(eventName + ':success', payload));
}

$(document.body).trigger($.Event(eventName + ':complete', payload));
},

on: function(actionName, fn) {
$(document.body).on('action:' + actionName, fn);
},

off: function(actionName, fn) {
$(document.body).off('action:' + actionName, fn);
},
};
}(r);

0 comments on commit d003c3a

Please sign in to comment.