-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathview_event.html
79 lines (62 loc) · 3.08 KB
/
view_event.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
---
title: Event Handling
section: view
layout: default
---
<h1>Event Handling</h1>
<p>
All Bolt <b>View</b>s have a <b>listen</b> method which can be used to listen for both DOM and custom events. Simply pass the event name and the function callback. The callback can be either a function or a string, in which case it is the name of a function on the <b>View</b>s owner. (For more information on the Owner concept, see <a href="view_building.html">Building Views</a>.
</p>
<p>
The example below shows the case where the listen function is passed a function. Note that the event name does not contain 'on', this is important. The <b>event</b> parameter passed to the callback is a <a href="http://javelinjs.com">Javelin</a> event, not a standard DOM event. This provides a number of extra functions that you can see in the <a href="http://www.phabricator.com/docs/javelin/class/JX.Event.html">Javelin Docs</a>. To get the original DOM event, call <b>event.getRawEvent()</b>.
</p>
{% highlight javascript %}
var Button = require('views/Button').Button;
var myButton = new Button();
myButton.listen('mouseover', function(event) {
console.log('mouseover event');
});
{% endhighlight %}
<h2>Using the Builder</h2>
<p>
It is also possible to attach events when using the <a href="view_building.html">Builder</a>. Any property set on a view passed the Builder that starts with <b>on</b> is assumed to be an event and is listened to. The example below shows a simple version of this in use. Note that unlike when calling the <b>listen</b> method directly, the event name begins with <b>on</b>.
</p>
{% highlight javascript %}
var Button = require('views/Button').Button;
var myButton = builder.build({
view: Button,
onmouseover: function(event) {
console.log('mouseover event');
}
});
{% endhighlight %}
<h2>Events in the Owner</h2>
<p>
Bolt provides a clean way of organizing event listeners when composing hierarchies of views. Rather than pass an actual function as a listener, instead pass the string name of a function in that views <b>owner</b> (see <a href="view_building.html">Building Views</a> for an explanation of the owner concept). This makes for a more readable view hierarchy declaration, as you don't have to pass actual functions, bind them to the view etc.
</p>
{% highlight javascript %}
var Container = require('container').Container;
require('javelin/core').createClass({
name : 'MyOwnerContainer',
extend: Container,
members: {
render: function() {
this.setLayout({
content: "Hello World!",
ref: "myInnerView",
// Add two listeners, passing the names of the functions that will handle their events
ontouchstart: "handleTouchStart",
ontouchend: "handleTouchEnd"
});
},
// Handler for the touch start event
handleTouchStart: function(event) {
this.refs.myInnerView.setContent("That tickles!!");
},
// Handler for the touch end event
handleTouchEnd: function(event) {
this.refs.myInnerView.setContent("Touch me again and I'll .....");
}
}
});
{% endhighlight %}