Last active
November 13, 2016 22:20
-
-
Save bsandhu/62fd74612a0a4444eeffe8e6e5b91bfa to your computer and use it in GitHub Desktop.
Register Knockout React components together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['log', | |
'knockout', | |
'modules/loans/ironBank/components/myReactComponent', | |
'modules/loans/commons/utils/knockoutReactBinding'], | |
function (log, | |
ko, | |
_myReactComponent) { | |
'use strict'; | |
function ComponentsRegistry() { | |
log.info('Init component registry'); | |
this.init(); | |
} | |
ComponentsRegistry.prototype.init = function () { | |
// This component can now be added to the DOM - assuming an existing KO app - like so: | |
// <my-component></my-component> | |
ko.components.register('my-component', wrapReactComponentInKO(_myReactComponent.MyComponent)); | |
}; | |
/** | |
* @See KnockoutReactBinding.js | |
*/ | |
function wrapReactComponentInKO(reactComponent) { | |
return { | |
// By default supply the React component to the template | |
viewModel: function () { | |
return {'reactComponent': reactComponent} | |
}, | |
// By default the component just has the react binding | |
template: '<div data-bind="react: reactComponent"/>' | |
} | |
} | |
return new ComponentsRegistry(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment