forked from nickshang/JavaScriptNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.component2.html
More file actions
76 lines (57 loc) · 2.19 KB
/
32.component2.html
File metadata and controls
76 lines (57 loc) · 2.19 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="../lib/knockout/dist/knockout.js"></script>
</head>
<body>
<div>
<h4>Second instance, passing parameters</h4>
<div data-bind='component: {
name: "message-editor",
params: { initialText: "Hello, world!" }
}'></div>
<h4>First instance, without parameters</h4>
<div data-bind='component: "message-editor"'></div>
</div>
<div id = "my-component-template">
Message: <input data-bind="value: text" />
length: <span data-bind="text: text().length"></span>
</div>
<script type="text/javascript">
var elemInstance = document.getElementById('my-component-template');
// ko.components.register('message-editor', {
// viewModel: function(params) {
// this.text = ko.observable(params && params.initialText || '');
// },
// template: { element: elemInstance }
// });
// AMD module whose value is a component viewmodel constructor
define(['knockout'], function(ko) {
function MyViewModel(params) {
this.text = ko.observable(params && params.initialText || '');
}
return MyViewModel;
});
ko.components.register('message-editor', {
viewModel: {
createViewModel: function(params, componentInfo) {
// - 'params' is an object whose key/value pairs are the parameters
// passed from the component binding or custom element
// - 'componentInfo.element' is the element the component is being
// injected into. When createViewModel is called, the template has
// already been injected into this element, but isn't yet bound.
// - 'componentInfo.templateNodes' is an array containing any DOM
// nodes that have been supplied to the component. See below.
// Return the desired view model instance, e.g.:
return new MyViewModel(params);
}
},
template: { element: elemInstance }
});
ko.applyBindings();
</script>
</body>
</html>