How to handle project switching

Related to glitch-restart-button

How to check that user opened a project?
When you change project by Switch Project button, hashchange event doesn’t fire.

video
- here event fires only when you manually change URL in searchbar

That’s how I handle it for now:

window.addEventListener('hashchange', async () => await listener())

Maybe Glitch has own handlers for this event? Thanks for support

Hey @jarvis394 that’s an interesting question, thanks!

We use the o_0 library for observables in the Editor, which offers the observe function to attach a listener. For example when a new project is loaded if you look in the browser console you’ll see the project domain and id logged. This is handled by code like this:

self.projectIsLoaded.observe(function(value) {
    if (value) {
      self.logger().log(`🙋 ${self.currentProject().domain()}: `, self.currentProject().id());
      self.customDomainPreviewHostName('');

      if (self.editorIsEmbedded() && self.projectIsMemberOrMoreForCurrentUser()) {
        return self.notifyEditInEmbed(true);
      }
    }
  });

where self is the application object. So you could write something like this:

application.projectIsLoaded.observe( function( val ) { if (val) { alert('Project switched!'); } } );

Is that useful?

1 Like

Yes, I’ve already realized that, but thank you anyway!

UPD: Stuck on stupid error, always wait for DOM load!!!