0
使用DOM的执行加伯方法:如何将此Coffeescript拆分为单独的文件?
SITENAME = {
common: {
init: function() {
// application-wide code
}
},
users: {
init: function() {
// controller-wide code
},
show: function() {
// action-specific code
}
}
};
UTIL = {
exec: function(controller, action) {
var ns = SITENAME,
action = (action === undefined) ? "init" : action;
if (controller !== "" && ns[controller] && typeof ns[controller][action] == "function") {
ns[controller][action]();
}
},
init: function() {
var body = document.body,
controller = body.getAttribute("data-controller"),
action = body.getAttribute("data-action");
UTIL.exec("common");
UTIL.exec(controller);
UTIL.exec(controller, action);
}
};
$(document).ready(UTIL.init);
我怎么能使用单独的.coffee文件翼滑轨管道这种技术?
我想要做以下
users.js.coffeee
SITENAME =
users:
init: ->
alert("nope")
index: ->
alert("hi")
但如果我这样做了,它只是重写,并迫使我把它全部在一个文件中。
我该如何拆分它并在此技术中为每个页面使用命名空间?