Coder Social home page Coder Social logo

Comments (3)

Alhadis avatar Alhadis commented on June 18, 2024

When I call just update, it doesn't init recently added folds

When you say "init recently added folds", what are you referring to exactly?

from accordion.

lBodia avatar lBodia commented on June 18, 2024

@Alhadis, sorry, I explained it a bit unclear.
I didn't create any folds, I just dynamically change accordion items, by adding and deleting HTML items from it. And to make those items work, I had to init this accordion again.
I'm just wondering, is it possible to destroy the previous accordion object, because I saw that it was still kept in accordions array.

from accordion.

Alhadis avatar Alhadis commented on June 18, 2024

Short answer: no it's not possible because it shouldn't be needed. Accordions can be disabled and reenabled programmatically after construction, which is (almost) the same as destroying and recreating it.

What you're experiencing is a limitation caused by a pretty stupid oversight on my behalf... one I'm surprised I never noticed earlier — there's no way of dynamically adding or removing accordion folds.

I'll need to fix this. In the meantime, you can use this ugly and illogical workaround:

Click to show
// Usage:
let el = document.createElement("li");
el.innerHTML = "<h3>Foo</h3><div>Bar</div>";
addFold(el, document.querySelector(".accordion"));

function addFold(element, accordion){
	if(!(accordion instanceof Accordion))
		accordion = Accordion.getAccordion(accordion);
	const prev = accordion.folds[accordion.folds.length - 1];
	const fold = new prev.constructor(accordion, element);
	accordion.folds.push(fold);
	prev.nextFold = fold;
	fold.previousFold = prev;
	if(element.parentElement !== accordion.el)
		accordion.el.appendChild(element);
	accordion.update();
	return fold;
}

function removeFold(fold){
	if(fold instanceof HTMLElement)
		fold = Accordion.getFold(fold);
	const {parentElement} = fold.el;
	parentElement && parentElement.removeChild(fold.el);

	const {folds} = fold.accordion;
	const index = folds.indexOf(fold);
	if(~index){
		const prev = folds[index - 1];
		const next = folds[index + 1];
		folds.splice(index, 1);
		prev.nextFold = next;
		if(next)
			next.previousFold = prev;
	}
	fold.accordion.update();
}

As for destroying an Accordion instance... well, as mentioned before, it shouldn't be necessary, but now that I think about it, it would benefit applications needing to perform a clean teardown of their state. That's unrelated to this issue, however.

from accordion.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.