knockout / knockout

Knockout makes it easier to create rich, responsive UIs with JavaScript
http://knockoutjs.com/
Other
10.45k stars 1.52k forks source link

Implementing Knockout components dependency injection using Typescript #2101

Open Kwirirai opened 8 years ago

Kwirirai commented 8 years ago

Is there a way to implement dependency injection using Knockout Components. I would like to be able to swap different implementations of a service in a component. For example I would like to use different implementation of a service that implements a Store. One service can use ajax while another service can use HTML storage. The reason I need this is to be able to quickly mock up my app on the client side without worrying about the server side using the HTML storage and then when everything is fine I can hook up the ajax service to test the integration.

chrisknoll commented 8 years ago

Use a module loader (like requireJS) where you can define the module ID of your service layer, but in the require.js config, alias the moduleID to your mock:

requirejs.config( {
  "paths": {
    "sericeModuleId": "modules/mocks/serviceModule" // change this to your real implementation when you have it.
  }
});
Kwirirai commented 8 years ago

Thanks @chrisknoll I am using Webpack for bundling which has its own AMD loader. I will try and see if I can play around with the Webpack configurations.