简体   繁体   中英

How to identify plugin (croppie or any other) initialized?

I use croppie for editing pictures before uploading. The task is to destroy and init again this plugin without reloading the page.

So what I've already had:

$('#someID').croppie({
   viewport: {
      width: 128,
      height: 128
   }
});
$('#someID').croppie('bind', {
   url: 'some base64 encoded image'
});

After some operations - I hide the plugin:

$('#someID').toggle();
$('#someID').croppie('bind');

After all there are cases when I need croppie again and first part of js code works again, so I have in console:

croppie.min.js:1 Uncaught Error: Croppie: Can't initialize croppie more than once

And obviously I just hafta check initialized plugin for current element now or not. What I found:

// it supposed to be the answer, however it doesn't work correctly for me.    
if (!jQuery().fn.croppie) {

       $('#someID').croppie({
           viewport: {
                width: 128,
                height: 128
           }
       });

    }

this answer - and it always true ('cause of it's included library and it returns function) and I can't check this way; this one the same issue.

So, how to make sure that plugin initialized now or not?

PS It could be croppie or any other plugin. Thank you!

Instead of if (!jQuery().fn.croppie) , check with if(!$('#someID').data('croppie'))

if(!$('#someID').data('croppie'))
{
 $('#someID').croppie({
       viewport: {
            width: 128,
            height: 128
       }
   });

}

In croppie.js only comment the condition at line 1487

if (element.className.indexOf('croppie-container') > -1){
  throw new Error("Croppie: Can't initialize croppie more than once");
}

This works for me!!

if(!$('#someID').hasClass('croppie-container')){
    $ImageCrop = $('#someID').croppie({
        enableExif: true,
        viewport: viewportval,
        boundary: boundaryval
    });        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM