GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

[Question] Asset Manger not updating #2743

Closed andy-aware closed 4 years ago

andy-aware commented 4 years ago

I've read the API and it says that we can filter assets by attributes. Cool! but I'm having trouble getting the asset manager to update:

I'm running the following:

editor.on('component:selected', component => {
    const am = editor.AssetManager;

    let assets = am.getAll().filter(
        asset => asset.get('category') == 'cat-1'
    );

    console.log("Img in Cat-1: " + assets.length);
    am.render(assets);
});

I have 2 images loading in my asset manager on editor.init -

grapesjs.init({
    assetManager: {
        assets: [
            {category: "cat-0", src: "img.jpg", width: "x", height: "x"},
            {category: "cat-1", src: "img.jpg", width: "x", height: "x"},
        ]
    },
});

both images have the attribute:

category: 'cat-X'

I want to just load cat-1 but this just doesn't work. Console logging works fine and returns the item but the attribute manager doesn't. What am I doing wrong?

Thanks

artf commented 4 years ago

Hi @awaredigital I can't see any issue on my side. If you open the Assets modal and run this code in the official demo you'll see it updating correctly

const am = editor.AssetManager;
am.render(
    am.getAll().filter(item => /image(1|2|3).jpg$/.test(item.get('src')))
);

So probably you'll need to create a reproducible demo with your case to see what is wrong