-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathknockout-bind-react.js
More file actions
36 lines (32 loc) · 1.02 KB
/
knockout-bind-react.js
File metadata and controls
36 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['knockout', 'react', 'react-dom'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('knockout'), require('react'), require('react-dom'));
} else {
factory(root.ko, root.React, root.ReactDOM);
}
}(this, function (ko, React, ReactDOM) {
ko.bindingHandlers.react = {
init: function (element) {
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
ReactDOM.unmountComponentAtNode(element);
});
return {
controlsDescendantBindings: true
};
},
update: function (element, valueAccessor, allBindings) {
var options = ko.unwrap(valueAccessor());
if (options && options.component) {
var componentInstance = ReactDOM.render(
React.createElement(options.component, options.props),
element
);
if (options.ref) {
options.ref(componentInstance);
}
}
}
};
}));