knockout / tko

🥊 Technical Knockout – The Monorepo for Knockout.js (4.0+)
http://www.tko.io
Other
275 stars 34 forks source link

Add support for promise resolution in createViewModel function of components #77

Open rposener opened 6 years ago

rposener commented 6 years ago

I find that it would be really helpful in a few situations if I could inject a promise (which maybe doing ajax and/or indexeddb work) just before my component is displayed. This is somewhat supported by the AMD/require syntax, but does not seem to be simple/straightforward. It would be helpful if we could return a promise from createViewModel, that is awaited before the component is created and instantiated.

What I see the team always doing instead is creating some "loaded" Observable that is set after the component initialization finishes. This creates extra paints in some cases that just seem unnecessary - especially when working with indexeddb, or just a cached ajax get. The 3-5 ms wait triggering multiple paints, plus when tracking dirty, the issue of dealing with new viewmodel state change (after construction) seems like a lot of effort could be simplified if createViewModel supported returning a promise that returns a viewmodel. That would let us deal with many of these issues in a clean and natural way right at component instantiation. Since the component loader is asyncronous anyway, seems like this shouldn't be too hard to add.

caseyWebb commented 6 years ago

Related: https://github.com/knockout/tko/issues/64

brianmhunt commented 6 years ago

I get it. Will look into it, for sure. I feel like reworking the loader in general, too.

Just to ensure I’m on the right page, can you put together a codepen or jsfiddle that shows how a sample case and comments the expected behaviour?

rposener commented 6 years ago

@brianmhunt - I think this should be as simple a codepen I can get that gives an example of what and how it should be supported. Welcome to other's ideas/inputs as well codepen.