Plyr: v3 and multiple instances

Created on 2 Mar 2018  路  8Comments  路  Source: sampotts/plyr

The new version exports only Plyr which has no setup method.

All 8 comments

You shouldn't upgrade to major software releases and expect them to not break the API.

Changelog etc here: #766

This in particular should be of interest to you: https://github.com/sampotts/plyr/blob/beta/readme.md#javascript-1

I know but it still mentions the NodeList example.

This does not work:

const player = new Plyr(document.querySelectorAll('.js-player'));

From the documentation section I linked to:

"A NodeList or Array of HTMLElement - the first element will be used"

This does what you want:

const players = Array.prototype.map.call(document.querySelectorAll('.js-player'), el => new Plyr(el));

I know. We did it with a loop over the NodeList but the example makes not much sense when the full NodeList is not traversed at all ;-)

Then don't use NodeList...

Yep, .setup() was removed in favour of a proper class and one to one relationship with element vs instance. It was getting a little messy with allowing whatever in and then spitting out an array with one or many instances back. This way is cleaner and as you've found out if a developer wants to setup multiple players it's as easy as...

const players = Array.from(document.querySelectorAll('.js-player')).map(player => new Plyr(player));

The example is just to show a NodeList is _accepted_ as a value. It doesn't necessarily make it the _best_ for any scenario. It's really because I added support for passing jQuery objects which are always an array of elements as I'm sure you know.

I'll update the docs to make this a little clearer.

I'll update the docs to make this a little clearer.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Lycanthrope picture Lycanthrope  路  4Comments

muuvmuuv picture muuvmuuv  路  3Comments

nam-co picture nam-co  路  4Comments

TheZoker picture TheZoker  路  4Comments

Generalomosco picture Generalomosco  路  3Comments