Julienh / Sharrre

Make your sharing widget! Sharrre is a jQuery plugin that allows you to create nice widgets sharing for Facebook, Twitter, Google Plus (with PHP script) and more.
sharrre.com
MIT License
1.04k stars 413 forks source link

render function called twice #297

Open FrancoisDucobu opened 7 years ago

FrancoisDucobu commented 7 years ago

Hello,

First, thanks for this awesome plugin. Setting up sharing button has never been easier.

But I've got a problem with the plugin.

I'm using the 'template' option and for an unknown reason, the render function is call twice.

here

/src/js/jquery.sharrre.js -- line 102

if (self.options.template !== '') {  //for personalized button (with template)
    this.options.render(this, this.options);
}

and here

/src/js/jquery.sharrre.js -- line 203

if (shareCount === this.options.shareTotal) {
    this.options.render(this, this.options);
}

Here is my js

$('#share-button').sharrre({
    share: {
        twitter: true,
        facebook: true
    },
    buttons: {
        facebook: {layout: 'box_count'},
        twitter: {count: 'vertical'}
    },
    title: $(this).data('href'),
    url: $(this).data('url'),
    template: '<ul class="list-inline social-icons"><li><div class="item"><a href="#" class="facebook"><i class="fa fa-facebook"></i></a></div></li><li><div class="item"><a href="#" class="twitter"><i class="fa fa-twitter"></i></a></div></li></ul>',
    enableHover: false,
    enableTracking: false,
    render: function(api, options){
        console.log('render');
    render: function(api, options){
        console.log('render');
        $(api.element)
            .off('click')
            .on('click', '.twitter', function() {
                api.openPopup('twitter');
            })
            .on('click', '.facebook', function() {
                api.openPopup('facebook');
            });
    }
    }
});

to avoid popup opening twice, I add ".off('click')" on the render function. But I'd rather understand where is my mistake.

Thanks,

Francois