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")
但如果我這樣做了,它只是重寫,並迫使我把它全部在一個文件中。
我該如何拆分它並在此技術中爲每個頁面使用命名空間?