Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

How to use fwEvents in JavaScript?

Open dk-wordpress opened this issue 10 years ago • 10 comments

Hi

Regarding documentation, i can attach custom event listener to any Option, for examplehttp://manual.unyson.io/en/latest/options/built-in/image-picker.html i want "on click" event for Image Picker option

i register custom JS script

wp_register_script( 'my-backend', get_template_directory_uri() . '/js/admin/admin.js' );        
wp_enqueue_script( 'my-backend', array( 'fw-events', 'jquery' ) );

then in JavaScript

jQuery.noConflict()( function($){
    "use strict";

        /** Image Picker On Change, not working **/
        fwEvents.on('fw:option-type:image-picker:clicked', function( e, data ) {
            console.log('it works!');
        });

});

when i click on Image Picker, i don't see "it works" in my console

Could you tell me, how to use FW Events? Thank you

dk-wordpress avatar Sep 27 '16 07:09 dk-wordpress

  1. Try to enable debug by typing fwEvents.debug(true) in one of your JS files
  2. Check JavaScript console in Chrome and see if your event listeners are added and if the events are triggered correctly.

Good luck.

andreiglingeanu avatar Sep 27 '16 10:09 andreiglingeanu

@andreiglingeanu events are in the option js , they don't trigger

https://github.com/ThemeFuse/Unyson/blob/master/framework/includes/option-types/image-picker/static/js/scripts.js#L21

danyj avatar Sep 27 '16 10:09 danyj

@andreiglingeanu thanks, but there is no errors in the console

dk-wordpress avatar Sep 27 '16 10:09 dk-wordpress

@danyj Then you have to listen for events on the option type DOM element itself. Or you are welcome to come up with a pull request that will trigger an event with fwEvents.

andreiglingeanu avatar Sep 27 '16 10:09 andreiglingeanu

@WPJobs

  $('.fw-option-type-image-picker').on('fw:option-type:image-picker:clicked', function( e, data ) {
            console.log('it works!');
        });

http://prntscr.com/cmwh28

danyj avatar Sep 27 '16 10:09 danyj

@andreiglingeanu , just saw that https://github.com/ThemeFuse/Unyson/blob/master/framework/includes/option-types/image-picker/static/js/scripts.js#L2

danyj avatar Sep 27 '16 10:09 danyj

@andreiglingeanu, @danyj, thank you, i figured it out, correct code to use:

fwEvents.on('fw:options:init', function (data) {

    $('.fw-option-type-image-picker').on('fw:option-type:image-picker:changed', function( e, data ) {
        console.log('image picker changed!');
    });

});

p.s. it is good to add this code in documentation :) thank you again for the fast support!

dk-wordpress avatar Sep 27 '16 11:09 dk-wordpress

(function($, fwe) {


    fwe.on('fw:options:init', function(data) {
        $('.fw-option-type-image-picker').on('fw:option-type:image-picker:changed', function(e, data) {
            console.log('image picker changed!');
        });

    });

})(jQuery, fwEvents);

danyj avatar Sep 27 '16 11:09 danyj

either way , it works ok

danyj avatar Sep 27 '16 11:09 danyj

Hi the code above apply for text option ?

(function($, fwe) {


    fwe.on('fw:options:init', function(data) {
        $( '.my-input' ).on('fw:option-type:text:changed', function(e, data) {
            console.log( 'not print!' );
        });

    });

})(jQuery, fwEvents);

not works for me

notrealdev avatar Feb 05 '18 05:02 notrealdev