I am trying to get a better understanding of the modular design pattern and I am using Requirejs to manage my dependencies.
I understand separating code into modules (using define with RequireJS) so they take on only single responsibilities and also splitting them into separate JavaScript files so Require JS can load them.
In my main.js, the entry points looks like this (which depends on work module). For brevity, I have left out the require.config, paths, etc.:
require(
['work'],
function(work) {
var url = "urlstring";
work.buildHTMLTable(url);
}
);
the method buildHTMLTable will append html table to a div tag.
I want to build a re-usable generic js library that will handle vertical/horizontal scroll event and arrow key navigation event. I need direction on how to incorporate these events. The below method is working, but I wanted to make sure I was setting everything up correctly.
Should I have one define method that has a function which takes care of handling the scrolling like so?
define('scrollingGrid', ['jquery'], function ($) { var bindScroll = function (hs, vs) { $(hs).on('scroll', function (e) { } $(vs).on('scroll', function (e) { } }; return { bindScroll: bindScroll }; } );
and then I can call it from the main as so:
require( ['work', 'grid'], function(work) { var url = "urlstring"; work.buildHTMLTable(url); grid.bindScroll('#hscroll', '#vscroll'); } );
Should I also handle the arrow key navigation in the same
scrollingGrid
module (see below)?require( ['work', 'grid'], function(work) { var url = "urlstring"; work.buildHTMLTable(url); grid.bindScroll('#hscroll', '#vscroll'); grid.bindArrow('#tab'); } );
In summary, I am creating a re-usable JavaScript control that could handle events on an HTML table. Is what I am doing okay or is there a better way?