Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Last active August 21, 2019 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjohnson/2ac84238ac13ead605ae972c827804b1 to your computer and use it in GitHub Desktop.
Save kjohnson/2ac84238ac13ead605ae972c827804b1 to your computer and use it in GitHub Desktop.
// Create a new object for custom validation of a custom field.
var mySubmitController = Marionette.Object.extend( {
initialize: function() {
this.listenTo( Backbone.Radio.channel( 'forms' ), 'submit:response', this.actionSubmit );
},
actionSubmit: function( response ) {
// Do stuff here.
},
});
// On Document Ready...
jQuery( document ).ready( function( $ ) {
// Instantiate our custom field's controller, defined above.
new mySubmitController();
});
@kristjankoppel
Copy link

I've tried this now on two different WordPress sites, and on a clean WP install and i get this error on console:
Uncaught ReferenceError: Marionette is not defined

Do anybody have any clue or idea what has changed?

@justinestrada
Copy link

I get the same error

@thpapag
Copy link

thpapag commented Apr 23, 2018

Any news on this error?

@mmichealjroberts
Copy link

You likely have included your script before the <?php wp_footer(); ?> tag in your footer.php template - you need to include it after this tag (i.e., after the Marionette object has been injected by Ninja Forms).

@geertvdheide1
Copy link

You likely have included your script before the <?php wp_footer(); ?> tag in your footer.php template - you need to include it after this tag (i.e., after the Marionette object has been injected by Ninja Forms).

Adding to this: even if you've added your custom script that holds this gist in the proper way - meaning through wp_enqueue_script and wp_footer - the error may still show up. It's important to have your custom script be included after any dependencies, and Wordpress allows us to set this up like so:

wp_enqueue_script( 'my-custom-scripts', get_template_directory_uri() . "/my-folder/my-script.js", array('jquery', 'nf-front-end'), false, true );

This assures your custom script runs after 'nf-front-end', which in turn depends on 'nf-front-end-deps' which contains Marionette. Including your custom script like this makes sure the code runs after both Backbone and Marionette are loaded, solving the issue.

@gauravblaggan
Copy link

Add your code inside this form ready function because once the form is loaded then the error will be solved
jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
Add your code here...
});

@dbloomsday
Copy link

You can simply move all of the code below "jQuery( document ).ready( function( $ ) {" and it should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment