Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug with get_script_depends / before_enqueue_scripts #1666

Closed
ProgressionStudios opened this issue May 1, 2017 · 7 comments
Closed
Assignees
Labels
question* [DEPRECATED] Indicates when an issue is used to ask a Question.

Comments

@ProgressionStudios
Copy link

ProgressionStudios commented May 1, 2017

Using the Hello World Plugin example for showing custom JS per widget (https://github.com/pojome/elementor-hello-world). I noticed the JavaScript doesn't load when the frontend editor is open:

I replaced the /assets/js/hello-world.js file with the code below to make a simple test changing the background color to yellow:

( function( $ ) { $(".elementor-widget-container").css("background-color", "yellow"); } )( jQuery );

Screen-shot when viewing normally: http://i.imgur.com/noWYvsQ.png
Screen-shot when front-end editor open (No yellow highlight): http://i.imgur.com/RWAy3vo.png

Let me know if this is a bug or of I am doing something wrong.

@ProgressionStudios ProgressionStudios changed the title Possible bug with get_script_depends/before_enqueue_scripts Possible bug with get_script_depends / before_enqueue_scripts May 1, 2017
@KingYes
Copy link
Member

KingYes commented May 3, 2017

Are you write your JS code like this structure: https://github.com/pojome/elementor-hello-world/blob/master/assets/js/hello-world.js ?

@ProgressionStudios
Copy link
Author

I replaced all the code in the hello-word.js with:

( function( $ ) { $(".elementor-widget-container").css("background-color", "yellow"); } )( jQuery );

Can you provide a simple code example with the above code using the hello-world example (Simple background color change)? It sounds like I'm not understanding your example located here: https://github.com/pojome/elementor-hello-world/blob/master/assets/js/hello-world.js

@KingYes
Copy link
Member

KingYes commented May 3, 2017

You don't use global jQuery selector on your widget. You need to based on $scope variable.

@KingYes KingYes self-assigned this May 3, 2017
@KingYes KingYes added the question* [DEPRECATED] Indicates when an issue is used to ask a Question. label May 3, 2017
@ProgressionStudios
Copy link
Author

ProgressionStudios commented May 3, 2017

Unfortunately I don't understand what you are saying. I am trying to enqueue a javascript file only when a specific widget is called. If I use the default WordPress wp_enqueue_scripts, it works great on both frontend and backend. However I don't want the javascript to show up on all pages when it's not needed. I assumed the solution was to use:

add_action( 'elementor/frontend/after_register_scripts', function() { wp_register_script( 'hello-world', plugins_url( '/assets/js/hello-world.js', ELEMENTOR_HELLO_WORLD__FILE__ ), [ 'jquery' ], false, true ); } );

and then adding get_script_depends for the widget area:

public function get_script_depends() { return [ 'hello-world' ]; }

But it sounds like I will also need to wrap additional javascript around the code and refer to the $scope variable? I am either missing the concept or it is very difficult to do a simple enqueue of a javascript file per widget (For example including a prettyPhoto lightbox file for one widget).

Perhaps you can put a simple example together using the code above for the hello-world widget code I was testing (that may help out as I don't understand the $scope variable and what is required)?

$(".can-be-any-class-just-an-example").css("background-color", "yellow");

PS: I really appreciate the help and responsiveness.

@KingYes
Copy link
Member

KingYes commented May 4, 2017

Like this:

( function( $ ) {
	var WidgetHelloWorldHandler = function( $scope, $ ) {
		$scope.find( '.can-be-any-class-just-an-example' ).css( 'background-color', 'yellow' );
	};
	
	// Make sure you run this code under Elementor..
	$( window ).on( 'elementor/frontend/init', function() {
		elementorFrontend.hooks.addAction( 'frontend/element_ready/hello-world.default', WidgetHelloWorldHandler );
	} );
} )( jQuery );

Just change the hello-world to your Widget ID..

@michellomp
Copy link

@KingYes I had a question when you have
var rellax = new Rellax('.rellax', {center: true});
how do you base it on $scope variable ?
Thanks

@KingYes
Copy link
Member

KingYes commented Jun 29, 2017

@michellomp:

Like this:
var rellax = new Rellax( $scope.find( '.rellax' ), { center: true } );

@KingYes KingYes closed this as completed Jun 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question* [DEPRECATED] Indicates when an issue is used to ask a Question.
Projects
None yet
Development

No branches or pull requests

3 participants