We have a JS file (basic-page.js) that has the following Drupal Behavior. In this, we created what we believe is considered a Drupal object (need help with correct term) that contains a function that can be called. In the latest request, we need the ability to reuse the same code from the Drupal Behavior in another Drupal Behavior.
How could we reuse the code? Currently the article.js is not able to use the code from basic-page.js, no errors or warnings are given.
basic-page.js (where Drupal object resides)
(function($) {
'use strict';
Drupal.customObj = {
'function1': function(element) {
/* perform some logic here */
},
'function2': function(var) {
/* perform some other logic here */
}
};
Drupal.behaviors.customName = {
attach: function(context, setting).once('customName').each(function() {
/* behavior runs here */
var func1 = Drupal.customObj;
var results = func1.function1($('body'));
//process results
});
};
})(jQuery);
article.js (where we want to reuse Drupal.customObj.function1)
(function($) {
'use strict';
Drupal.behaviors.articleResults = {
attach: function(context, setting).once('articleResults').each(function() {
/* behavior runs here */
var customObj = Drupal.customObj;
var results = customObj.function1($('body'));
//process results
});
};
})(jQuery);
attach: function(context, setting).once('articleResults').each(function() {
specifically). Are you sure those files are in the page?