question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slick.js reload the slider on image asset change

See original GitHub issue

Hello everyone, I am integrating slick slider, the issue is with reloading the slider after image is uploaded.

After onclick of open assets the Slick slider must be reinitialized so that regular html of the slider would become slick slider, that happens on adding the component to the canvas only: $(‘#slickslider’).slick({ dots: true,

infinite: true,
speed: 300,
arrows:true,
adaptiveHeight: true

});

That works in two cases: When i add new slider to canvas When i upload new image and then drag and move the component it reinitializes

The code:

	editor.DomComponents.addType('slickslider', {

model: { init() {

    //TESTING 
           this.listenTo(this, 'change:attributes', model =>{
                                    $('#slickslider').slick({
   dots: true,

infinite: true,
speed: 300,
arrows:true,
adaptiveHeight: true

}); }) //No results

        },

defaults: {
     components: [	
			{
				highlightable: true,
				editable: true,
				selectable: true,
				resizable:true
			}],
			
 
     traits: [
  
				    
		{
					    	type: 'buttonCarousel',
					    	label: 'Upload',
						name: 'upload1',
					    
					value:'Upload 1',
	
					},
						
							{
						type: 'select',
						label: 'reload',
						name: 'reload',
					
					options:[{
					    value:'yes',
					    name:'yes'
					},{
					    value:'no',
					    name:'no'
					}],
					
						changeProp: 0
					}
				
				
  ]}},

 view:{
        
		tagName: 'div',
		init({ model }) {


    $('#slickslider').slick({
   dots: true,

infinite: true,
speed: 300,
arrows:true,
adaptiveHeight: true

});

// RESULT: ignores this code

this.model.listenTo(this.model, 'change:attributes', model => {
				this.render();
			}); // RESULT: does not works

editor.render(); // Breaks the editor } }

} )

editor.BlockManager.add('slickslider', {
	label: 'Slick Slider',
	category: 'Media',
	attributes: { icon: 'fa fa-video' },
	content: {
	    
	    	type: 'slickslider',
		activeOnRender: 1,
							 style: {
  'background-color': '#000000',
},
	    script: function(){
	        
	        
	         var initMySLider = function() {

    $('#slickslider').slick({
   dots: true,

infinite: true,
speed: 300,
arrows:true,
adaptiveHeight: true

});

}

var script = document.createElement('script');
script.onload = initMySLider;
script.src = '//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js';
document.body.appendChild(script);


var link  = document.createElement('link');
link.rel  = 'stylesheet';
link.type = 'text/css';
link.href = 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.css';

var link2  = document.createElement('link');
link2.rel  = 'stylesheet';
link2.type = 'text/css';
link2.href = 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick-theme.css';

document.body.appendChild(link); document.body.appendChild(link2);

	    },
	    

  content: `<div  id="slickslider" data-gjs-type="slickslider"> <div class="slide" id="slide1"> <img src="oldimage1.png" > </div><div class="slide" id="slide2"> <img src="oldimage.png"   > </div></div>`

} });

})

Result: After uploading new image, both images are shown as a regular html:

image

After I move the component to up it becomes a slick js slider showing: new image uploaded and old image as a slider showing slick js arrows

image

Any suggestion would be helpful.

TLDR:

how to reinitialize ‘script’ function inside editor.blockmanager.add to reload the jquery code of slider?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
abzal0commented, Mar 18, 2020

@pouyamiralayi this is incredibly valuable code, thanks a lot for sharing it 😃 it works better than I needed. Will be sending you an email, hopefully you will reply.

1reaction
pouyamiralayicommented, Mar 18, 2020

Hi @abzal0 the correct function is actually updateScript my bad! here is how i did it:

first defining the component type:

    domc.addType('slick-type', {
        model: {
            defaults: {
                ccid: '',
                tagName: 'div',
                resizable: true,
                components: [
                    {
                        type: 'image',
                    },
                    {
                        type: 'image',
                    }
                ],
                script: function () {
                    const id = '{[ ccid ]}'
                    try {
                        $('#' + id).slick('unslick');
                    } catch (e) {
                    }
                    $('#' + id).slick({
                        dots: true,
                        infinite: true,
                        speed: 300,
                        arrows: true,
                        adaptiveHeight: true
                    })
                }
            },
        },
        view: {
            init() {
                const ccid = this.model.ccid
                this.model.set('ccid', ccid)
                const viewObj = this
                const am = editor.AssetManager;
                const tImageView = am.getType('image').view;
                am.addType('image', {
                    view: {
                        onClick() {
                            tImageView.prototype.onClick.apply(this);
                            viewObj.updateScript()
                            this.em.get('Modal').close();
                        },
                    }
                })
            }
        }
    })

Every time the script runs, i reset the slickjs for the sake of re initialization which you can see in the script. I have also used image type directly as my component’s children so i don’t have to manually call the open-assets command on a custom trait! For the last thing, I have overwritten the asset manager onClick event to call our script. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Reload slick (slider/carousel) jQuery plugin?
Last, show it using slick jQuery plugin to show HTML element as slider. However, after click 1st button, if i click 2nd button...
Read more >
Lazy Loading Images using Slick Slider - Solodev
In Part 4 of our Slick Slider Series, we will walk you through the process of lazy loading your slider images using Slick...
Read more >
Slick JS Slider: Learn how to easily add a carousel to your site
In this tutorial, you'll learn how to use Slick JS to easily add a slider style carousel to your web site. Recommended training...
Read more >
Slick Carousel Responsive slidesToShow Recipe
This markup contains 4 slide content containers for the carousel. The content container include FPO images and a heading.
Read more >
Image Carousels and Sliders: Why You Shouldn't Use Them ...
Should you use a carousel in your site? What the tests say · We have tested rotating offers many times and have found...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found