Jump to content

JavaScript changes for plugins


Luke

Recommended Posts

Hi guys,

 

In the dev branch there are some changes to be aware of. We're beginning to make moves towards dropping jQuery, and right now this is beginning with ajax and promises. There are some changes you need to make for the dev branch, although if you follow my instructions they will actually be compatible with both the stable and dev versions. Please note - you have plenty of time as this is not going into a beta release anytime soon. 

 

Ajax:

 

If you are using jQuery ajax methods, please drop this in favor of the browser's native fetch Api. You can read about fetch here:

 

https://davidwalsh.name/fetch

 

It's extremely developer friendly so you really shouldn't have any problems. For example:

// Simple response handling
fetch('/some/url').then(function(response) {
	
}).catch(function(err) {
	// Error :(
});

For browsers that haven't implemented fetch, we are providing a polyfill (a library with the exact same signature), so either way it will just work.

 

Promises:

 

If you are using ApiClient methods, you will now receive native promises instead of jQuery deferred objects. This change is very simple - you need to change your .done(fn) and .fail(fn) callbacks to use .then(fn, fn).

 

So for example

ApiClient.getUsers().done(fn).fail(fn)

becomes:

ApiClient.getUsers().then(fn, fn);

and that's it. And the best news of all - jQuery deferred supports both of these signatures which means these changes work on both the stable and dev servers. I have already taken care of every plugin in our repository so you can check that out for examples:

 

https://github.com/MediaBrowser/Emby.Plugins

Link to comment
Share on other sites

If you would like to get a head start on other changes towards dropping jQuery, then you can remove all use of $(...) in favor of the native methods - document.querySelectorAll(..), as well as event binding - $(elem).on('click', fn) -> elem.addEventListener('click', fn); 

 

It will be a little while before that becomes mandatory, so for now, that's just extra credit :)

Link to comment
Share on other sites

In my plugin I have for example:

 

$('devicebtn').on('click', function....

 

Is this an example of what will need to change?

Link to comment
Share on other sites

In my plugin I have for example:

 

$('devicebtn').on('click', function....

 

Is this an example of what will need to change?

 

Eventually but it can be left for now. 

  • Like 1
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...