Problem using fabrik.form.elements.added

Bauer

Well-Known Member
Whenever I include javascript inside a form_##.js file and include this function
Code:
Fabrik.addEvent('fabrik.form.elements.added', function (block) {
     [js code here]
});
the code only gets run the first time the form is loaded in a session. Or is this by design?

According to the Wiki ( http://fabrikar.com/forums/index.php?wiki/form-javascript-objects/#form-events )...
fabrik.form.elements.added (elements) // All elements have been added to the form's formElements property

To duplicate - create a menu item to a fabrik list. Then in a form_##.js file for that list include this code.
Code:
Fabrik.addEvent('fabrik.form.elements.added', function (block) {
     console.log('start of fabrik.form.elements.added');
     alert('this is js code inside fabrik.form.elements.added');
     console.log('end of fabrik.form.element.added');
});
Click the menu item and edit a row in the list. Save the form then click the menu item (or edit icon in the list) again.

The js code inside the fabrik.form.elements.added (for me anyhow) is only working when the form is first loaded
 
I call this "Phil's Law".
It's working fine for me now too.
Sorry for taking up your time.

But I know I've had it happen to me before too. Go figure.
Just wondering - is your Fabrik.addEvent inside a jQuery(document).ready(function(){ -or- a requirejs(['fab/fabrik'], function () { ???
Mine is now inside a requirejs(['fab/fabrik'], function () {
Isn't that where it should be?
Maybe I had it different before.
 
I always use requirejs(), it's the only reliable way to run code that needs to run after the main Fabrik object is set up. And even though the main Fabrik class doesn't have a lot of code in it, it's deliberately light weight, the DOM is still usually ready before Fabrik is initialized. Remember that jQuery's ready() function is equivalent to Moo's domready event, it fires when the DOM is ready, so when the HTML in the page itself has been processed and the document model created, but before other assets (images, scripts, etc.) are loaded.

And within Fabrik itself, things get a lot more complicated with JS dependencies, as we have an almost infinite number of permutations of what JS gets loaded, and what depends on what to be able to run. So we use the requirejs dependency manager to handle that, whereby everything in Fabrik which uses Javascript registers itself and it's dependencies with requirejs. And the first thing to get loaded is 'fab/fabrik', which then triggers everything that directly depends on that to run (like registering a Fabrik event handler) which in turn trigger anything that rely on those things to run.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top