Help fixing JS after migrating to 3.1

Status
Not open for further replies.

rackem

Well-Known Member
I had some custom Javascript that was working in F3 but now it is not after updating to F3.1. I see that some changes were made
http://fabrikar.com/forums/index.php?wiki/upgrading-from-3-0-to-3-1/

So I am trying to follow the instructions here
http://fabrikar.com/forums/index.php?wiki/adding-custom-javascript/

I got my code working on a new record but I get an error when editing an existing record. Basically, all my code does is insert the repeat group number into a field when a repeat group is added.

Here is my code. Note my custom code is just the part between the *********:
Code:
// Ensure that Fabrik is loaded
// Make sure to match the form_X matches (2 places!)
requirejs(['fab/fabrik'], function () {
 
  // The block you want to use
  var blockRef = 'form_24';
 
  // Should we use an exact match for the blockRef?
  var exact = false;
 
  // Bool to ensure ini() is only called once
  var setup = false;
 
  // Add an event to watch for when blocks are added.
  Fabrik.addEvent('fabrik.block.added', function () {
    if (setup) {
      return;
    }
    var block = Fabrik.getBlock(blockRef, exact);
 
    // If we find our form, we should call the ini() method
    if (block !== false) {
        setup = true;
        ini(block);
      }
    });
 
  // **************************************************************
  // Put your code here
 
 
    //  Inserts the group number as the default value for the place field
    Fabrik.addEvent('fabrik.form.group.duplicate.end', function(form, event, g_id, r_count){
        var places = $('group' + g_id).getElements('input[name *= mps_pub_results___place]');
        var new_id = places[r_count].id;
        Fabrik.blocks['form_24'].formElements.get(new_id).update(r_count + 1);
    });
 
 // **************************************************************
  var ini = function (block) {
    console.log('ini form_24.js', block);
  }
});

The error occurs in the line

Code:
Fabrik.blocks['form_24'].formElements.get(new_id).update(r_count + 1);

On new records, Fabrik.blocks['form_24'] is defined.

However when editing a record, I need Fabrik.blocks['form_24_X'] where X is the record id. How do I get that? It seems that the script given in the instructions takes care of this somehow but I am missing how to get it working.

I also discovered that the wiki link is broken on this page
http://fabrikar.com/forums/index.php?wiki/form-javascript/

that used to go to here
http://fabrikar.com/forums/index.php?wiki/Fabrik_3_form_javascript/
 
OK, that seems to be working. Thanks!

My next question on the same topic:
I have an element in my form that calls a function in my custom JS file. It was working fine in F2.5 however now I recieve an error that my function is not defined.

Here is the JS in the element which called on Change
Code:
sumNetCash();

My custom JS file contains
Code:
function sumNetCash(){
       // My code here
    }

Is it not possible to call custom functions anymore or is there a new syntax?
 
its probably a question of scope, I am guessing you have your custom js file as follows:

JavaScript:
requirejs(['fab/fabrik'], function () {
function sumNetCash(){
      // My code here
    }
});

that would make the function sumNetCash only available inside the requirejs function. Calling the function from outside won't work. So you could do:

JavaScript:
requirejs(['fab/fabrik'], function () {
 
});
 
function sumNetCash(){
      // My code here
    }

or assign the function to the window object :

JavaScript:
requirejs(['fab/fabrik'], function () {
  window.sumNetCash = function(){
      // My code here
    }
});
 
You are correct Rob, thanks! Once I moved my function outside of the requirejs function, I was able to get it working again. I thought it was a best practice sort of thing to keep my code in there to keep it from getting hacked or something.

The wiki entries regarding JS are really confusing IMO but I'm not sure where to begin to improve it. A lot of that I suppose is the differences between Fabrik 2.x, 3.x, and now 3.1.

Here are some broken links at least in case valuable information is hidden there.
http://fabrikar.com/forums/index.php?wiki/adding-custom-javascript/
http://fabrikar.com/forums/index.php?wiki/Fabrik_3_form_javascript/
I can update the wiki links if it is simply the case that the information on these pages was just moved?
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top