如果我们有:
#app/assets/javascripts/tabbed_panels.js
var new_items = function() {
#do something
});
$(document).on("turbolinks:load", new_items);
和
#app/assets/javascripts/paginate_users.js
var new_users = function() {
#do something
});
$(document).on("turbolinks:load", new_users);
等
有没有办法避免为每个脚本重复$(document).on("turbolinks:load", '#');
?
答案 0 :(得分:1)
我不认为为同一事件排队多个函数是错误的模式或违反DRY原则,但您可以将行为封装在单个函数中,以避免让各个模块知道Turbolinks的加载行为:
var initialize = function(callback) {
$(document).on('turbolinks:load', callback);
}
# later...
initialize(new_users);