-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenyobasics.html
193 lines (179 loc) · 7.33 KB
/
enyobasics.html
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<div id="main" class="clearfix two-columns-right">
<div id="intro">
<ol class="hp-breadcrumbs"><li><a href="/" target="_top" onfocus="blurLink(this);">Home</a></li><li><a href="https://web.archive.org/web/20121023072434/https://developer.palm.com/content/resources.html" target="_top" onfocus="blurLink(this);">Resources</a></li><li class="ctx-last">Building Your First App</li></ol>
<h1 class="page-title">Enyo Basics</h1>
<ul class="inline-icon-list"><li><a onclick="window.print(); return false;" href="javascript: void(0);" class="icon-print">Print</a></li><li><a href="" target="_top" class="icon-mail addthis_button_email">Email</a></li><li class="last"><div class="addthis_toolbox addthis_default_style"><a class="icon-share addthis_button_compact">Share</a></div></li></ul>
</div>
<div id="content" class="col-content">
<div class="default-content article-content">
<h2 id="enyo-basics---kinds-components-controls">
Enyo Basics - Kinds, Components, Controls
</h2>
<p>
In this document, we take a closer look at kinds, components, and controls--three foundational Enyo concepts that we touched upon in <a href="/?page=enyogroundup">Enyo From the Ground Up</a>.
</p>
<a id="cKinds"></a><h2 id="kinds">
Kinds
</h2>
<p>
In Enyo, nearly all code is located in object prototypes called <em>kinds</em>. A kind is an object constructor created using the <code>enyo.kind</code> factory method.
</p>
<p>
The kind in Enyo has a role analogous to that of the class in Java or C++. For example, kinds provide a standard mechanism for implementing inheritance, with subkinds inheriting properties and functions from their superkinds.
</p>
<p>
Here are examples of kinds that create objects representing points in two- and three-dimensional space. Note that the second kind (<code>Point3D</code>) inherits from the first (<code>Point</code>):
</p>
<blockquote>
<pre class="contentbox code-text">
<code>
enyo.kind({
name: "Point",
x: 0,
y: 0,
constructor: function(x, y) {
this.x = x;
this.y = y;
},
translate: function(dx, dy) {
this.x += dx;
this.y += dy;
},
toString: function() {
return this.x + ", " + this.y;
}
});
enyo.kind({
name: "Point3D",
kind: "Point",
z: 0,
constructor: function(x, y, z) {
this.inherited(arguments);
this.z = z;
},
translate: function(dx, dy, dz) {
this.inherited(arguments);
this.z += dz;
},
toString: function() {
return this.inherited(arguments) + ", " + this.z;
}
});
p = new Point3D(1, 1, 1);</code>
</pre>
</blockquote>
<a id="cComponents"></a><h2 id="components">
Components
</h2>
<p>
<code>Component</code> objects are the basic building blocks of Enyo. Components share common features, which allow them to work together in a standard way. For example, all components have a name (string). A component may create other components, which it is said to <em>own</em>. Each component maintains a collection of the components it owns, and is responsible for the lifecycle of those components.
</p>
<p>
Here are the kind definitions of two components. At runtime, a <code>SimulatedMessage</code> object creates (and thus owns) a <code>RandomizedTimer</code> object, which it uses to simulate the sending of service messages at random intervals:
</p>
<blockquote>
<pre class="contentbox code-text">
<code>
enyo.kind({
name: "RandomizedTimer",
kind: enyo.Component,
baseInterval: 100,
percentTrigger: 50,
events: {
onTriggered: ""
},
create: function() {
this.inherited(arguments);
this.job = window.setInterval(enyo.hitch(this, "timer"), this.baseInterval);
},
destroy: function() {
window.clearInterval(this.job);
},
timer: function() {
if (Math.random() < this.percentTrigger * 0.01) {
this.doTriggered();
}
}
});
enyo.kind({
name: "SimulatedMessage",
kind: enyo.Component,
components: [
{name: "timer", kind: RandomizedTimer, percentTrigger: 10,
onTriggered: "timerTriggered"}
],
timerTriggered: function() {
this.log("Simulated Service Message Occurred");
}
});</code>
</pre>
</blockquote>
<a id="cControls"></a><h2 id="controls">
Controls
</h2>
<p>
A <code>Control</code> object is a component that controls a DOM node (i.e., an element in the user interface). Controls are generally visible and the user often interacts with them directly. Things like buttons and input boxes are obviously controls, but in Enyo a control may become as complex as an entire application.
</p>
<p>
In the following example, we define a <code>Circle</code> control and put it to use inside a <code>TrafficLight</code> control:
</p>
<blockquote>
<pre class="contentbox code-text">
<code>
enyo.kind({
name: "Circle",
kind: "Control",
published: {
color: "magenta",
bgColor: "black"
},
content: "Hi",
style: "padding: 2px 6px; border: 3px solid; border-radius: 20px;
cursor: pointer;",
create: function() {
this.inherited(arguments);
this.colorChanged();
},
colorChanged: function() {
this.applyStyle("border-color", this.color);
},
bgColorChanged: function() {
this.applyStyle("background-color", this.bgColor);
},
mousedown: function() {
this.applyStyle("background-color", "white");
},
mouseup: function() {
this.applyStyle("background-color", "black");
}
});
enyo.kind({
name: "TrafficLight",
kind: "Control",
style: "position: absolute; padding: 4px; border: 1px solid black;
background-color: silver;",
components: [
{kind: "Circle", color: "red", onclick: "circleClick"},
{kind: "Circle", color: "yellow", onclick: "circleClick"},
{kind: "Circle", color: "green", onclick: "circleClick"}
],
circleClick: function(inSender) {
var lights = {red: "tomato", yellow: "#FFFF80", green: "lightgreen"};
if (this.lastCircle) {
this.lastCircle.setBgColor("black");
}
this.lastCircle.setBgColor(lights[inSender.color]);
this.lastCircle = inSender;
}
});</code>
</pre>
</blockquote>
</div>
</div>
<div class="col-aside">
<div class="sidebox">
<h3>Contents</h3>
<ol class="article-nav"><li class="current last"><a href="" target="_top">Building Your First App</a><ol class="level-1"><li><a href="#cKinds" target="_top">Kinds</a></li><li><a href="#cComponents" target="_top">Components</a></li><li class="last"><a href="#cControls" target="_top">Controls</a></li></ol></li></ol>
</div>
</div>
</div>