Jump to content

jQuery events as pure Javascript for plugin config


pünktchen

Recommended Posts

pünktchen

I need some help to translate these jQuery events into pure Javascript:

$('.pluginConfigurationPage').on('pageinit', function() {
  //create other event handlers here
})

$('.pluginConfigurationPage').on('pageshow', function() {
  var page = this;
  loadConfig(page);
})

For "pageinit" i didn't find any pure JS equivalent at all. For "pageshow" i have tried this one:

document.getElementById('pluginConfigurationPage').addEventListener("pageshow", function (event) {
  var page = document.getElementById(event.currentTarget.id);
  loadConfiguration(page);
});

The problem is this only works the first time i visit the plugin config screen.
If i switch to lets say "Logs" and come back, the event isn't fired anymore and i have to refresh the browser page.

Link to comment
Share on other sites

This works if you've got your HTML and JS in separate files. It's basically the same with js embedded in the html, but I think you get a 'page' object instead of a 'view':

define(["loading", "emby-input","emby-select"],
function (loading) {
	return function(view) {
		view.addEventListener('viewshow', () => {
			//do stuff
		});
	}
});

 

For the getElementById, you need to use view.querySelector() instead (or querySelectorAll() when there's more than one element) instead of trying to mess with document.

Someone else will need to explain the whys of it, but basically, there ends up being more than one version of your plugin html in the dom and the only way to reliably target the active one is to always stay withing the page/view object Emby gives you.

Link to comment
Share on other sites

Quote

 but basically, there ends up being more than one version of your plugin html in the dom

Yes if the user navigates away and then comes back, there's two instances now. state is kept in the document for a few views so that when you return everything is restored as it was. So all functions need to be scoped to the current view.

Link to comment
Share on other sites

12 minutes ago, Luke said:

Yes if the user navigates away and then comes back, there's two instances now. state is kept in the document for a few views so that when you return everything is restored as it was. So all functions need to be scoped to the current view.

Thanks for the explanation. I had figured out that it was happening and accepted that it was reality but didn't know the reason.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...