tracelytics/pageguide — pull request #28 by arizzitano (2014-03-08 03:49:34+00:00)
Title: Idempotence

On one-page applications, depending on the context in which it's set up, pageguide may be run multiple times on the same page. This can cause major issues with conflicting event handlers, multiple `ready()` timers, repeated (or lost) DOM elements, etc.

Ideally, pageguide should always `init()` in a clean state, without having to worry about conflicting with existing handlers or elements. I already added a bit of support for this in 6841ead, removing any existing `#tlyPageGuideWrapper` elements, but it really needs to be taken a step further:
- [x] Before creating any DOM elements, check for previously created ones and remove them
- [x] Unbind any outstanding handlers before re-binding them
- [x] Add a `destroy` method
- [ ] Consider storing the pageguide object in the global scope inside `tl.pg`, so the object itself can be deleted safely before creating a new one*
- [x] Handle new pageguide-related elements (`#tlyPageGuide`, `#tlyPageGuideWelcome`) gracefully -- persist them if necessary, but if new ones are detected on the page, use those instead

I have done significant work on this in Traceview's copy of pageguide, which I can port over to this repo.

\* Is there ever a case where multiple pageguide objects on a single page are desirable? I don't think there is, but am happy to accommodate if anyone has a good argument for allowing it.