angular-chosen icon indicating copy to clipboard operation
angular-chosen copied to clipboard

Initialization fails when asynchronous load is slow

Open Crossbow78 opened this issue 8 years ago • 1 comments

Description

In the scenario where the list is asynchronously loaded, and this loading takes 'too long', the dropdown will stay empty.

What seems to happen is this: During pageload the dropdown gets initialized with the (still empty) html select list. When the dropdown items eventually arrive from the server, we'll populate the list on the Angular viewmodel, which in turn modifies the (hidden) html select list options. However, this change is not picked up by the Chosen dropdown.

Work-around (sort of)

A manual refresh after populating the Angular viewmodel works: setTimeout(function() { $('#dropdown').trigger('chosen:updated'); }, 50); ...but this goes against the Angular model driven design. You would need to insert view-specific code into the controller which is ugly. On top of that, an artificial delay is needed in order to allow Angular apply the viewmodel changes to the DOM.

Reproduction

This can be easily reproduced on your own example page when using Google Chrome with network throttling enabled in the Developer Tools (eg. use the "Regular 2G" profile). Refresh the page, and all dropdowns will stay empty.

Crossbow78 avatar Nov 17 '16 16:11 Crossbow78

A $setTimeout is already being used in the $watchGroup callback here Will have to dig deeper. Please go ahead if you want to pick this issue up. I'm currently unavailable to pick this, but will look into it soon. @Crossbow78 thanks for pointing this out.

adityasharat avatar Nov 17 '16 16:11 adityasharat