wp-shortcake / shortcake

Shortcake makes using WordPress shortcodes a piece of cake.
GNU General Public License v2.0
664 stars 142 forks source link

Event Attribute Callbacks - Fires Twice. #798

Open 89gsc opened 6 years ago

89gsc commented 6 years ago

I have a check box which I have registered a JS callback for in order to hide / show some fields, as you can see there are a couple console logs in there to help me debug.

The problem with this is that for each change in the checkbox so unchecking it or checking it this code fires twice. with some odd results. See below the code for what I am getting as a console output.

function overrideTermChecked (changed, collection, shortcode) {

        function attributeByName (name) {
            return _.find(
                collection,
                function (viewModel) {
                    return name === viewModel.model.get('attr');
                }
            );
        }

        var updatedVal    = changed.value,
            override_term = attributeByName('term'),
            override_msg  = attributeByName('content');

        console.log(typeof updatedVal);
        console.log(updatedVal);
        if (updatedVal) {
            console.log('Should Show');
            override_term.$el.show();
            override_msg.$el.show();
        } else {
            console.log('Should Hide');
            override_term.$el.hide();
            override_msg.$el.hide();
        }
    }

    wp.shortcake.hooks.addAction('glossary_tooltip.override', overrideTermChecked);

image

So I am not able to make the 2 text fields for term / content show they remain hidden all the time. Is this an issue with my implementation?

akkatanp commented 6 years ago

I too am facing the same issue. Any solution?

89gsc commented 6 years ago

I never sound a solution to this problem. I changed how I handled this by not using events and did somthing else which wont work in most cases for other users. Sorry I couldn't be more help.