tko icon indicating copy to clipboard operation
tko copied to clipboard

Add support for promise resolution in createViewModel function of components

Open rposener opened this issue 7 years ago • 3 comments

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.

rposener avatar Jun 24 '18 22:06 rposener

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

caseyWebb avatar Jun 25 '18 00:06 caseyWebb

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?

brianmhunt avatar Jun 25 '18 00:06 brianmhunt

@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.

rposener avatar Jun 25 '18 13:06 rposener