I have written a singleton module which is to be used for some future modifications on .chart
elements. Modification functions, such as colorArgumentButtons()
are called recursively so that I don't have to iterate over all .chart
elements in all functions.
Is my use of _
for private "class vars" proper? Otherwise they may conflict with my local vars.
Am I using the revealing module pattern correctly and is my namespace implementation okay? Feel free to comment on bad design choices!
var SuperHappy = (function (_parent, $) {
var _my = _parent.Chart = _parent.Chart || {};
// default settings
var _settings = {
autoFind: true,
chartSelector: '.chart'
};
// jQuery vars
var _$charts = $();
_my.init = function (options) {
/// <summary>
/// Initializes the module.
/// </summary>
/// <param name="options">Custom JSON options</param>
_settings = $.extend(_settings, options);
if (_settings.autoFind) {
_my.findCharts();
}
};
var _applyFunctionToAll = function (func, $charts) {
/// <summary>
/// Applies the supplied function for each chart element in the collection.
/// </summary>
/// <param name="func">Function to be applied</param>
/// <param name="$charts">Optional chart collection to apply function on</param>
if (typeof $charts === 'undefined' && _$charts !== $()) {
$charts = _$charts;
}
if ($charts.length === 1) {
return false;
}
$charts.each(function () { // can this be converted to a for loop?
func($(this));
});
return true;
};
_my.colorArgumentButtons = function (color, $chart) {
/// <summary>
/// Colors all argument buttons.
/// </summary>
/// <param name="color">Color to be used</param>
/// <param name="$chart"></param>
if (_applyFunctionToAll(_my.colorArgumentButtons.bind(null, color), $chart)) {
return;
}
$chart.find('button').css('background-color', color);
};
_my.findCharts = function (selector) {
/// <summary>
/// Finds all chart elements.
/// </summary>
/// <param name="selector">Optional CSS selector for finding chart elements</param>
selector = typeof selector !== 'undefined' ? selector : _settings.chartSelector;
_$charts = $(selector);
};
return _parent;
})(SuperHappy || {}, jQuery);
jQuery(document).ready(function ($) {
SuperHappy.Chart.init();
SuperHappy.Chart.colorArgumentButtons('#f00');
});