Skip to content

Instantly share code, notes, and snippets.

@rpendleton
Last active October 20, 2015 00:44
Show Gist options
  • Save rpendleton/4b6281e88baef91ecca9 to your computer and use it in GitHub Desktop.
Save rpendleton/4b6281e88baef91ecca9 to your computer and use it in GitHub Desktop.
Allows intercepting require.js define events. This is useful in userscripts that modify sites created using require.js
/* jshint -W097 */
/* global require: true */
'use strict';
// If you want to hide categories from the budget overview, you can put their
// id's here. They will still be visibile on the budgets page.
var hide_budgets = [1404, 506, 204, 103];
//! require.js events
var initialize = function() {
require(['underscore'], function(_) {
var context = require.s.contexts._;
var module = context.Module;
var events = context.config.events;
// we need to intercept init so that we can add custom event listeners to require.js modules
var oldInit = module.prototype.init;
module.prototype.init = function() {
var self = this;
// if we have listeners that haven't been added yet, add them now
if(events && events[this.map.id]) {
_.each(events[this.map.id], function(handlers, type) {
_.each(handlers, function(handler) {
self.on(type, handler.bind(self));
});
});
// remove the temporary event listeners now that they've been added
delete events[this.map.id];
}
return oldInit.apply(this, arguments);
};
});
};
if(typeof require === 'function') {
require(['libs'], initialize);
require.s.contexts._.config.events = {};
}
else {
require.callback = initialize;
require.deps = ['libs'];
require.events = {};
}
// onRequire function
// Purpose: stores an event listener in the require.js config. it will be attached once the module is defined.
// Parameters: the id of the module, the event type, and the callback.
// Returns: none
var onRequire = function(id, event, callback) {
var events = require.s ? require.s.contexts._.config.events : require.events;
events[id] = events[id] || {};
events[id][event] = events[id][event] || [];
events[id][event].push(callback);
};
//! implementation examples
//! mint-better-account-overview
// swaps the institution name with the account name in the left sidebar
onRequire('views/authenticated/overview/modules/accounts/CashAccountView', 'defined', function(cashView) {
var old = cashView.prototype.initialize;
cashView.prototype.initialize = function() {
old.apply(this, arguments);
this._swapNames = false;
};
});
// removes desired categories from buget overview
onRequire('legacy/js/overview/budget/OverviewBudgetList', 'defined', function() {
/* global $M: true, _:true */
var prototype = $M.OverviewBudgetList.prototype;
var oldSetData = prototype.setData;
prototype.setData = function(data) {
data.budgets = _.filter(data.budgets, function(budget) {
return (hide_budgets.indexOf(budget.category.id) === -1);
});
return oldSetData.apply(this, arguments);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment