can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
320 lines (268 loc) • 11.9 kB
HTML
<!--####################################################################
THIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN
INSTEAD CHANGE:
source: [object Object]
@page HandleUserInteraction
######################################################################## -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>CanJS - Handle User Interaction</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" type="text/css" href="./static/bundles/bit-docs-site/static.css">
<link rel="shortcut icon" sizes="16x16 24x24 32x32 48x48 64x64" href="/docs/images/canjs_favicon.ico">
<link rel="apple-touch-icon" sizes="57x57" href="../docs/images/canjs_favicon_57x57.png">
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="../docs/images/canjs_favicon_57x57.png">
<link rel="apple-touch-icon" sizes="72x72" href="../docs/images/canjs_favicon_72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="../docs/images/canjs_favicon_114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="../docs/images/canjs_favicon_128x128.png">
<link rel="apple-touch-icon" sizes="144x144" href="../docs/images/canjs_favicon_144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="../docs/images/canjs_favicon_152x152.png">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta name="apple-mobile-web-app-status-bar-style" content="white-translucent">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-2302003-11', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger"/>
<label for="nav-trigger">Menu</label>
<div id="everything">
<div id="left" class="column">
<div class="top-left">
<div class="brand">
<div class="logo">
<a href="../index.html" alt="CanJS"></a>
<div class="dropdown project-dropdown">
<a href="https://donejs.com/">DoneJS</a>
<a href="http://stealjs.com/">StealJS</a>
<a href="http://jquerypp.com/">jQuery ++</a>
<a href="https://funcunit.com/">FuncUnit</a>
<a href="http://documentjs.com/">DocumentJS</a>
</div>
</div>
<div class="version">
<div class="version-number">
3.0.0
</div>
<div class="dropdown version-dropdown">
<a href="https://v2.canjs.com">2.3.27</a>
</div>
</div>
</div>
<div class="search-bar">
<p>
</p>
</div>
</div>
<div class="bottom-left">
<div class="social-side-container">
<ul class="social-side">
<li>
<a class="header-mobile github" href="https://github.com/canjs/canjs" target="_blank"><img class="social-icon-small" src="../docs/images/github.png">Github</a>
</li>
<li>
<a class="header-mobile twitter" href="https://twitter.com/canjs" target="_blank"><img class="social-icon-small" src="../docs/images/twitter.png">Twitter</a>
</li>
</ul>
<ul class="social-side">
<li>
<a class="header-mobile" href="https://gitter.im/canjs/canjs" target="_blank">Chat</a>
</li>
<li>
<a class="header-mobile" href="http://forums.donejs.com/c/canjs" target="_blank">Forum</a>
</li>
</ul>
</div>
</div>
</div>
<div id="right" class="column">
<div class="top-right">
<div class="top-right-top">
<ul class="top-right-bitovi">
<li class="dropdown">
<a href="http://bitovi.com" class="bitovi icon-bits">Bitovi</a>
<ul class="dropdown-menu">
<li><a href="http://bitovi.com">Bitovi.com</a></li>
<li><a href="http://bitovi.com/blog/">Blog</a></li>
<li><a href="http://bitovi.com/consulting/">Consulting</a></li>
<li><a href="http://bitovi.com/training/">Training</a></li>
<li><a href="http://bitovi.com/open-source/">Open Source</a></li>
</ul>
</li>
</ul>
<div class="brand">
<div class="logo">
<a href="../index.html" alt="CanJS"></a>
</div>
</div>
<ul class="top-right-links">
<li>
<a href="https://gitter.im/canjs/canjs">Chat</a>
</li>
<li>
<a href="http://forums.donejs.com/c/canjs">Forum</a>
</li>
<li>
<a class="github-button nav-social" href="https://github.com/canjs/canjs" data-count-href="/canjs/canjs/stargazers" data-count-api="/repos/canjs/canjs#stargazers_count">Star</a>
</li>
<li>
<a href="https://twitter.com/canjs" class="twitter-follow-button nav-social" data-show-count="true" data-show-screen-name="false">Follow @canjs</a><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</li>
</ul>
</div>
<div class="breadcrumb">
<li><a href="HandleUserInteraction.html">Handle User Interaction</a></li>
<li class="breadcrumb-dropdown">/ <a> On this page</a>
<ul class="on-this-page"></ul>
</li>
<div class="nav-toggle" title="Back to top"></div>
</div>
</div>
<div class="bottom-right">
<article>
<section class="title">
<div class="page-type">
<h1>Handle User Interaction</h1>
<div>page</div>
</div>
<section class="description">
</section>
</section>
<section class="on-this-page-table">
</section>
<section class="title-footer">
<ul class="title-links">
<!-- <li><a href="#">docco</a></li> -->
<li><a href="//github.com/canjs/canjs/tree/v3.0.0/docs/can-guides/recipes/handle-user-interaction.md">source</a></li>
<!-- <li><a href="#">download</a></li> -->
<!-- <li><a href="#">tests</a></li> -->
</ul>
</section>
<section class="body">
<p>When a user does something, such as clicking, an <code>event</code> occurs. Event handlers specify
how <a href="http://bitovi.com/blog/2010/10/a-crash-course-in-how-dom-events-work.html">JavaScript should respond to an event</a>.</p>
<p>This recipe will introduce handling a click event using a <a href="http://canjs.com/docs/can.Control.html"><code>can.Control</code></a>.
Using a list of people like previous recipes, clicking any individual person's name
will remove that person from the list.</p>
<p>Previous examples have used jQuery's event handlers:</p>
<pre><code>$("#push").click(function(){
//handle the event
});
</code></pre>
<p>CanJS provides a few different ways to respond to events. As well as
making application code simpler, using CanJS to handle events can help to
automatically prevent <a href="http://bitovi.com/blog/2012/04/zombie-apocolypse.html">memory leaks</a>.</p>
<p>To handle events, extend <code>can.Control</code>.</p>
<pre><code>var PeopleList = can.Control.extend({
//behavior
});
</code></pre>
<p>You create a <code>can.Control</code> by <a href="http://canjs.com/docs/can.Control.html#sig_newcan_Control_element_options_">calling it as a constructor function</a>.
The first argument is the element the control will be created on.
The second argument is an object of options.</p>
<pre><code>new PeopleList('#my-app', {people: people});
</code></pre>
<p>A <code>can.Control</code> handles events with functions declared with two arguments: an
element or list of elements (using a jQuery-style selector) and a specific event.
Below, this is 'li click', meaning when any <code>li</code> elements that are <code>clicked</code> the
function will be called to handle the click event.</p>
<pre><code>var PeopleList = can.Control.extend({
init: function( element, options ){
this.people = new can.List(options.people);
this.element.html( can.view('app-template', {
//defines people in the template as the observable can.List
people: this.people
}));
},
'li click': function( li, event ){
//Handle the click event
}
};
</code></pre>
<p>When the constructor function is called and the <code>can.Control</code>
is instantiated:</p>
<ol>
<li>The <code>init</code> method is called</li>
<li>An observable <code>can.List</code> is created from <code>people</code></li>
<li>The list is rendered using <code>can.view</code> so when the list changes, so will the view</li>
</ol>
<pre><code>var people = [
{firstname: "John", lastname: "Doe"},
{firstname: "Emily", lastname: "Dickinson"},
{firstname: "William", lastname: "Adams"},
{firstname: "Stevie", lastname: "Nicks"},
{firstname: "Bob", lastname: "Barker"}
];
</code></pre>
<p>When the event handler for a <code>click</code> runs, it needs a way
to access the object associated with the <code>li</code> that was clicked.
With the <a href="http://canjs.com/docs/can.stache.helpers.data.html"><code>data</code></a> helper,
the element will retain a reference
to the object it is associated with (in this case, a <code>person</code>).</p>
<pre><code><ul>
{{#each people}}
<li {{data 'person'}}>
{{lastname}}, {{firstname}}
</li>
{{/each}}
</ul>
</code></pre>
<p>Finally, the event handler must be defined. In a <code>can.Control</code>,
an event handler function <a href="http://canjs.com/docs/can.Control.html#section_Listeningtoevents">can be defined with a string containing
a selector and an event</a>.
In this case, these are <code>li</code> and <code>click</code>, respectively,
since we want to handle click events on each list item.</p>
<pre><code>var PeopleList = can.Control.extend({
init: function(){
...
},
'li click': function( li, event ) {
var people = this.people;
var person = li.data('person');
var index = people.indexOf(person);
people.splice(index, 1);
}
});
</code></pre>
<p>When a user clicks a list item:</p>
<ol>
<li>The function bound to <code>li click</code> is called</li>
<li>The object associated with that list item is accessed using the <code>data</code> helper</li>
<li>That 'person's data is removed from the observable list of <code>people</code></li>
<li>The template updates automatically</li>
</ol>
<p>As a reminder, though event handlers respond to actions on the page,
they should <em>change application state or data</em> (e.g. make a change to a <code>can.Map</code>)
rather than modifying the DOM directly (e.g. toggling a class).
This will update the page automatically, keeping code manageable.</p>
<p>This is <em>one</em> way to handle events. Others will be covered
in the following recipes while building widgets.</p>
<iframe width="100%" height="300" src="http://jsfiddle.net/donejs/F9kzt/embedded/result,html,js/" allowfullscreen="allowfullscreen" frameborder="0"> </iframe>
</section>
<script type="text/javascript">
var docObject = {"src":{"path":"docs/can-guides/recipes/handle-user-interaction.md"},"description":"\n","name":"HandleUserInteraction","title":"Handle User Interaction","type":"page","parent":"Recipes","order":4,"comment":" ","pathToRoot":".."};
</script>
</article>
<footer><p>CanJS is part of <a href="http://donejs.com" target="_blank">DoneJS</a>. Created and maintained by the core <a href="https://donejs.com/About.html#section=section_Team" target="_blank">DoneJS team</a> and <a href="http://bitovi.com" target="_blank">Bitovi</a>. <strong>Currently 3.0.0.</strong></p></footer>
</div>
</div>
</div>
<script>
steal = {
instantiated: {
"bundles/bit-docs-site/static.css!$css" : null
}
};
</script>
<script type='text/javascript' data-main="bit-docs-site/static" src="./static/node_modules/steal/steal.production.js"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</body>
</html>