Having read-up on require.js (via http://requirejs.org/docs/api.html) I realise this is pretty straight forward. A module can return its methods making them assessible to other parts of the code base. In this case I wanted to assess the method "bindUpDownButtons" (of utils/forms) and trigger it from mods/addAnother.
In require.js it can be done like this...
Forms.js looks a bit like this:
Code:
define(function(require){
// lots of code omitted
var NumberFields = {
// lots of methods relating to number fields
bindUpDownButtons : function($field){
// the code of interest
}
};
// export these methods
return { init : init, NumberFields : NumberFields };
});
Then in addAnother.js the important bit is here:
Code:
// ...
var addSet = function(config){
// code that sets up and adds new set here
// Special treatment for sets that contain number fields
if($newHtml.find('input[type=number], input[data-type=number]').length > 0){
// Ensure that custom number buttons are bound
require(['utils/forms'], function(forms) {
$newHtml.find('input[type=number], input[data-type=number]').each(function(){
forms.NumberFields.bindUpDownButtons($(this));
});
});
}
// rest of code
};
Hope this helps anyone who comes across the same lack of knowledge.
Cheers!
Bookmarks