Created
February 16, 2017 15:16
-
-
Save daftAnorak/a8d6458ff98ceffd7a81c6e7635ab05d to your computer and use it in GitHub Desktop.
Attempting to bind InfernoJS with a pre-existing Angular (v 1.5) app.
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
/** | |
* Angular Hook Module | |
* @module vNode2NgComponent | |
*/ | |
// ============================================================================ | |
// Imports | |
// | |
import R from 'ramda'; | |
import { infernoRender as iRender } from './utils'; | |
// ============================================================================ | |
// Internals / Angular binders | |
// | |
/** | |
* Returns the `Angular` Component `binding-type` based on the key passed. | |
* Keys prefixed with `on` will be treated as external functions. | |
* Keys ending with `s` will be treated as one-way objects/arrays. | |
* All other keys will be treated bound strings | |
* | |
* @example | |
* | |
* vNodeNgBinding('title'); // => '@?' | |
* vNodeNgBinding('onClick'); // => '&?' | |
* vNodeNgBinding('items'); // => '<?' | |
* | |
*/ | |
const vNodeNgBinding = R.cond([ | |
[R.test(/^on[A-Z]/), R.always('&?')], | |
[R.test(/s$/), R.always('<?')], | |
[R.T, R.always('@?')] | |
]); | |
/** | |
* Creates an `Angular` Component `bindings` object. | |
* | |
* @example | |
* | |
* var obj = vNodeNgBindings(['items','onClick','title']); | |
* // obj === { items: '<?', onClick: '&?', title: '@?' } | |
* | |
*/ | |
const vNodeNgBindings = R.converge( | |
R.zipObj, | |
[ | |
R.identity, | |
R.adjust(vNodeNgBinding) | |
] | |
); | |
/** | |
* Creates an `Angular` Component Controller wrapper. | |
* Binds InfernoJS VNode Rendering on Component lifecycle. | |
*/ | |
const vNodeCompCtrlFn = (fn, props) => (ngEl) => { | |
const vm = this; | |
const render = R.pipe( | |
R.always(vm), | |
R.pick(props), | |
fn, | |
iRender(R.__, ngEl[0]) | |
); | |
// binds vNode rendering to internal, angular-component lifecycle | |
vm.$postLink = render; | |
vm.$onChanges = render; | |
}; | |
/** | |
* Creates an `Angular` Component Instantiation Object. | |
* Used for attaching components to an an `Angular` module. | |
*/ | |
export const vNode2NgComponent = (fn, props) => ({ | |
bindings: vNodeNgBindings(props), | |
controller: ['$element', vNodeCompCtrlFn(fn, props)] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment