forked from fnando/dispatcher-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.js
54 lines (41 loc) · 1.37 KB
/
dispatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Dispatcher.js - jQuery Dispatcher
// Copyright (c) 2008-2012 Nando Vieira (nandovieira.com.br)
// Dual licensed under the MIT (MIT-LICENSE.txt)
// and GPL (GPL-LICENSE.txt) license
;(function($){
this.App = this.App || {};
var Dispatcher;
Dispatcher = this.Dispatcher = {};
Dispatcher.ALIASES = {
"create": "new",
"update": "edit",
"destroy": "remove"
};
Dispatcher.run = function() {
var dataPage = $('body').data('page')
, noDataPage = 'No data-page attribute found. Use something like <body data-page="controller#action" >'
;
if (dataPage.length === 0) {
throw(noDataPage);
}
var page = dataPage.split("#")
, controllerName = page[0]
, actionName = page[1]
;
actionName = Dispatcher.ALIASES[actionName] || actionName;
// Executed before every controller action
if (App.before) {
App.before();
}
if (App[controllerName]) {
// Executed before any action from the current controller
App[controllerName].before && App[controllerName].before();
// The current action per-se
App[controllerName][actionName] && App[controllerName][actionName]();
// The after callback for the current controller
App[controllerName].after && App[controllerName].after();
}
App.after && App.after();
};
$(document).ready(Dispatcher.run);
}).call(this, jQuery);