Can Knockout components be registered by a dynamically generated ?

Can Knockout components be registered by a dynamically generated <script>?



The motive is that I don't want to register all my components when the website is first visited by the browser. I want to be able to register components when a code needs it, and I don't want the JS file of the component to be loaded when the web page loads the very first time. Via Ajax, I want to load JS files containing component registration code.



Please, is this possible in Knockout?






that seems possible through RequireJS..

– Ray
Sep 16 '18 at 8:03






I don't want to have the RequireJS configuration beforehand. I don't want to have any clue about the component until an <a> calls a URL that a JS function then uses to load view model and template for the concerned component whose name should be indicated by the caller <a> (e.g. in a data attribute or something). There would be another JS code to hijack the URL call using History API without in order to prevent page reloads.

– Olumide
Sep 16 '18 at 8:21







There's a way to create custom component loader that can load, from URLs, view model data and template markup for a component. So, the question is about if I can call ko.components.register (using a JS code that has been supplied with a component name and the URLs for the view model and the template needed for the component) sometime later in code even long after the page has been loaded, so as to dynamically register component whose view model and template are loaded using a custom loader.

– Olumide
Sep 16 '18 at 8:40







And how should ko.applyBindings be called? With or without parameters? Or would the newly loaded component simply start working?

– Olumide
Sep 16 '18 at 8:49






The easy way out is to run the registration code for all my thousand components when a page loads, but then use them when needed. So, the question is still that how can I avoid having to register a thousand components but register them later on.

– Olumide
Sep 16 '18 at 8:59




1 Answer
1



If you want to load components in a different way than provided by Knockout's default loader (which uses ko.components.register), you should create a custom loader (docs). It seems that in your case, you'll just want to implement the getConfig method to return the configuration for a component.


ko.components.register


getConfig






Thank you. I had visited the docs on custom loaders. I am using the code examples in loading custom components that belong to the same category (e.g. partial views that supply the main content of a web page), which was the original problem that was hard for me to express in my question. I was able to use just one custom component name to load each and swap them. I didn't know you later responded. Thank you for being swift. I'll mark your answer as the answer and it is the foundation to build on. I've successfully started extended my code to do routing, etc. I'll share my progress later.

– Olumide
Sep 17 '18 at 11:36







I have worked on the routing. I couldn't post it very much earlier so that I could take some time to verify that all the code work as expected. I am through and have now posted a question about it on coderreview. It is a full ASP.NET MVC 5 project that does client-side routing using Knockout and HTML5 History API and can easily be made to work with any server-side web development framework. Thank you in anticipation of your help over there.

– Olumide
Oct 4 '18 at 12:54



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ