UNPKG

can

Version:

MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.

707 lines (459 loc) 22 kB
<!DOCTYPE html> <!--#################################################################### THIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN INSTEAD CHANGE: source: [object Object] @module can-list ######################################################################## --> <html lang="en"> <head> <meta charset="utf-8"> <title>CanJS - can-list</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> &nbsp; </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> <ul> <li class=" "> <a class="page" href="guides.html" title="Welcome to CanJS! These guides are here to help you develop and improve your relationship with CanJS. After all, picking a JavaScript framework is a commitment. We want CanJS to be the framework you marry. This page helps you know how advance through the different stages of this relationship:"> Guides </a> </li> <li class=" "> <a class="page" href="can-core.html" title="The best, most hardened and generally useful libraries in CanJS."> Core </a> </li> <li class=" "> <a class="page" href="can-ecosystem.html" title="Useful libraries that extend or add important features to the core collection."> Ecosystem </a> </li> <li class=" "> <a class="page" href="can-infrastructure.html" title="Utility libraries that power the core and ecosystem collection."> Infrastructure </a> </li> <li class=" parent expanded"> <a class="page" href="can-legacy.html" title="Former libraries that we still accept patches for, but are not under active development."> Legacy </a> <ul> <li class=" "> <a class="module" href="can-ejs.html" title="EJS provides live ERB-style client-side templates."> can-ejs </a> </li> <li class="current parent expanded"> <a class="module" href="can-list.html" title=""> can-list </a> <ul> <li> <span>Prototype</span> <ul> <li class=" "> <a class="function" href="can-list.prototype.attr.html" title="Get or set elements in a List."> attr </a> </li> <li class=" "> <a class="function" href="can-list.prototype.each.html" title="Call a function on each element of a List."> each </a> </li> <li class=" "> <a class="function" href="can-list.prototype.filter.html" title="Filter the elements of a List, returning a new List instance with just filtered items."> filter </a> </li> <li class=" "> <a class="function" href="can-list.prototype.map.html" title="Call a function on each element of a List and return a new List instance from the results."> map </a> </li> <li class=" "> <a class="function" href="can-list.prototype.reverse.html" title="Reverse the order of a List."> reverse </a> </li> <li class=" "> <a class="function" href="can-list.prototype.splice.html" title="Insert and remove elements from a List."> splice </a> </li> </ul> </li> <li> <span>Static</span> <ul> <li class=" "> <a class="property" href="can-list.Map.html" title="Specify the Map type used to make objects added to this list observable."> Map </a> </li> <li class=" "> <a class="function" href="can-list.extend.html" title=""> extend </a> </li> </ul> </li> </ul> </li> <li class=" "> <a class="module" href="can-map.html" title="Create observable objects."> can-map </a> </li> <li class=" "> <a class="module" href="can-map-backup.html" title=""> can-map-backup </a> </li> <li class=" "> <a class="module" href="can-map-define.html" title="Defines the type, initial value, get, set, remove, and serialize behavior for attributes of a Map."> can-map-define </a> </li> <li class=" "> <a class="module" href="can-view-href.html" title="Sets an element&#x27;s href attribute so that it&#x27;s url will set the specified attribute values on can-route."> can-view-href </a> </li> </ul> </li> </ul> </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="../index.html">CanJS</a></li> / <li><a href="can-legacy.html">Legacy</a></li> / <li><a href="can-list.html">can-list</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>can-list</h1> <div>module</div> </div> <section class="description"> </section> </section> <section class="on-this-page-table"> </section> <section class="title-footer"> <ul class="title-social"> <li> <a class="npm-button" href="https://www.npmjs.com/package/can-list"> <img src="https://img.shields.io/badge/npm%20package-3.0.0-brightgreen.svg" alt="npm package badge" /> </a> </li> <li> <a class="github-button nav-social" href="https://github.com/canjs/can-list" data-count-href="/canjs/can-list/stargazers" data-count-api="/repos/canjs/can-list#stargazers_count">Star</a> </li> </ul> <ul class="title-links"> <!-- <li><a href="#">docco</a></li> --> <li><a href="//github.com/canjs/can-list/tree/v3.0.0/docs/list.md">source</a></li> <!-- <li><a href="#">download</a></li> --> <!-- <li><a href="#">tests</a></li> --> </ul> </section> <div class="signature"> <h2 class="signature-title"> <code>new List([array])</code> </h2> <p>Create an observable array-like object.</p> <div class="parameters"> <h3 class="parameters-title">Parameters</h3> <ol> <li><b>array</b> <code>{Array}</code>: <p>Items to seed the List with.</p> </li> </ol> </div> <div class="returns"> <h3 class="returns-title">Returns</h3> <p> <code>{<a href="can-list.html" title="">can-list</a>}</code>: <p>An instance of <code>List</code> with the elements from <em>array</em>.</p> </p> </div> </div> <div class="signature"> <h2 class="signature-title"> <code>new List(deferred)</code> </h2> <div class="parameters"> <h3 class="parameters-title">Parameters</h3> <ol> <li><b>deferred</b> <code>{can.Deferred}</code>: <p>A deferred that resolves to an array. When the deferred resolves, its values will be added to the list.</p> </li> </ol> </div> <div class="returns"> <h3 class="returns-title">Returns</h3> <p> <code>{<a href="can-list.html" title="">can-list</a>}</code>: <p>An initially empty <code>List</code>.</p> </p> </div> </div> <section class="body"> <p>Use for observable array-like objects.</p> <h2>Use</h2> <p><code>List</code> is used to observe changes to an Array. <code>List</code> extends <code><a href="can-map.html" title="Create observable objects.">can-map</a></code>, so all the ways that you're used to working with Maps also work here.</p> <p>Use <a href="can-list.prototype.attr.html" title="Get or set elements in a List.">attr</a> to read and write properties of a list:</p> <pre><code>var hobbies = new List([&quot;JS&quot;,&quot;Party Rocking&quot;]) hobbies.attr(0) //-&gt; &quot;JS&quot; hobbies.attr(&quot;length&quot;) //-&gt; 2 hobbies.attr(0,&quot;JavaScript&quot;) hobbies.attr() //-&gt; [&quot;JavaScript&quot;,&quot;Party Rocking&quot;] </code></pre> <p>Just as you shouldn't set properties of an Map directly, you shouldn't change elements of a List directly. Always use <code>attr</code> to set the elements of a List, or use [can-list::push push], [can-list::pop pop], [can-list::shift shift], [can-list::unshift unshift], or <a href="can-list.prototype.splice.html" title="Insert and remove elements from a List.">splice</a>.</p> <p>Here is a tour through the forms of <code>List</code>'s <code>attr</code> that parallels the one found under <a href="can-map.prototype.attr.html" title="Get or set properties on a Map.">attr</a>:</p> <pre><code>var people = new List(['Alex', 'Bill']); // set an element: people.attr(0, 'Adam'); people[0] = 'Adam'; // don't do this! // get an element: people.attr(0); // 'Adam' people[0]; // 'Adam' // get all elements: people.attr(); // ['Adam', 'Bill'] // extend the array: people.attr(4, 'Charlie'); people.attr(); // ['Adam', 'Bill', undefined, undefined, 'Charlie'] // merge the elements: people.attr(['Alice', 'Bob', 'Eve']); people.attr(); // ['Alice', 'Bob', 'Eve', undefined, 'Charlie'] </code></pre> <h2>Listening to changes</h2> <p>As with <code>Map</code>s, the real power of observable arrays comes from being able to react to changes in the member elements of the array. Lists emit five types of events:</p> <ul> <li>the <em>change</em> event fires on every change to a List.</li> <li>the <em>set</em> event is fired when an element is set.</li> <li>the <em>add</em> event is fired when an element is added to the List.</li> <li>the <em>remove</em> event is fired when an element is removed from the List.</li> <li>the <em>length</em> event is fired when the length of the List changes.</li> </ul> <p>This example presents a brief concrete survey of the times these events are fired:</p> <pre><code>var list = new List(['Alice', 'Bob', 'Eve']); list.bind('change', function() { console.log('An element changed.'); }); list.bind('set', function() { console.log('An element was set.'); }); list.bind('add', function() { console.log('An element was added.'); }); list.bind('remove', function() { console.log('An element was removed.'); }); list.bind('length', function() { console.log('The length of the list changed.'); }); list.attr(0, 'Alexis'); // 'An element changed.' // 'An element was set.' list.attr(3, 'Xerxes'); // 'An element changed.' // 'An element was added.' // 'The length of the list was changed.' list.attr(['Adam', 'Bill']); // 'An element changed.' // 'An element was set.' // 'An element was changed.' // 'An element was set.' list.pop(); // 'An element changed.' // 'An element was removed.' // 'The length of the list was changed.' </code></pre> <p>More information about binding to these events can be found under <a href="can-list.prototype.attr.html" title="Get or set elements in a List.">attr</a>.</p> </section> <script type="text/javascript"> var docObject = {"src":{"path":"node_modules/can-list/docs/list.md"},"description":"\n","name":"can-list","type":"module","types":[{"type":"constructor"}],"inherits":"can","download":"can/list","test":"can/list/test.html","parent":"can-legacy","release":"2.0","package":{"_args":[[{"raw":"can-list@3.0.0","scope":null,"escapedName":"can-list","name":"can-list","rawSpec":"3.0.0","spec":"3.0.0","type":"version"},"/Users/kevin/dev/canjs"]],"_from":"can-list@3.0.0","_id":"can-list@3.0.0","_inCache":true,"_location":"/can-list","_nodeVersion":"6.7.0","_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/can-list-3.0.0.tgz_1476486259468_0.8222074022050947"},"_npmUser":{"name":"daffl","email":"daff@neyeon.de"},"_npmVersion":"3.10.3","_phantomChildren":{},"_requested":{"raw":"can-list@3.0.0","scope":null,"escapedName":"can-list","name":"can-list","rawSpec":"3.0.0","spec":"3.0.0","type":"version"},"_requiredBy":["/","/can-connect","/can-map-backup","/can-view-scope"],"_resolved":"https://registry.npmjs.org/can-list/-/can-list-3.0.0.tgz","_shasum":"f30de74b08843edad5d18958c2bb20b858ed4af3","_shrinkwrap":null,"_spec":"can-list@3.0.0","_where":"/Users/kevin/dev/canjs","author":{"name":"Bitovi","email":"contact@bitovi.com","url":"http://bitovi.com"},"bit-docs":{"dependencies":{"bit-docs-glob-finder":"^0.0.5","bit-docs-dev":"^0.0.3","bit-docs-js":"^0.0.3","bit-docs-generate-readme":"^0.0.8"},"glob":{"pattern":"**/*.{js,md}","ignore":"node_modules/**/*"},"readme":{"apis":"./docs/apis.json"},"parent":"can-list"},"browser":{"transform":["cssify"]},"browserify":{"transform":["cssify"]},"bugs":{"url":"https://github.com/canjs/can-list/issues"},"dependencies":{"can-construct":"^3.0.0","can-event":"^3.0.1","can-map":"^3.0.1","can-observation":"^3.0.1","can-util":"^3.0.1"},"description":"Observable lists","devDependencies":{"bit-docs":"0.0.6","can-map-define":"^3.0.0-pre.2","cssify":"^1.0.2","done-serve":"^0.2.0","donejs-cli":"^0.9.5","generator-donejs":"^0.9.0","jshint":"^2.9.1","steal":"^0.16.0","steal-qunit":"^0.1.1","steal-tools":"^0.16.0","testee":"^0.2.4"},"directories":{},"dist":{"shasum":"f30de74b08843edad5d18958c2bb20b858ed4af3","tarball":"https://registry.npmjs.org/can-list/-/can-list-3.0.0.tgz"},"gitHead":"2834073d0170944c7a46981794a1f9f3f9e7fd70","homepage":"http://canjs.com","keywords":["canjs","canjs-plugin","donejs"],"main":"can-list","maintainers":[{"name":"daffl","email":"daff@neyeon.de"},{"name":"justinbmeyer","email":"justin@bitovi.com"},{"name":"matthewp","email":"matthew@matthewphillips.info"},{"name":"sinjhin","email":"john@ardea.io"}],"name":"can-list","optionalDependencies":{},"readme":"ERROR: No README data found!","repository":{"type":"git","url":"git://github.com/canjs/can-list.git"},"scripts":{"build":"node build.js","develop":"done-serve --static --develop --port 8080","document":"bit-docs","jshint":"jshint ./*.js --config","postversion":"git push --tags && git checkout master && git branch -D release && git push","preversion":"npm test && npm run build","release:major":"npm version major && npm publish","release:minor":"npm version minor && npm publish","release:patch":"npm version patch && npm publish","release:pre":"npm version prerelease && npm publish","test":"npm run jshint && npm run testee","testee":"testee test/test.html --browsers firefox","version":"git commit -am \"Update dist for release\" && git checkout -b release && git add -f dist/"},"system":{"configDependencies":["live-reload"],"npmAlgorithm":"flat"},"version":"3.0.0"},"link":"../docco/list/list.html docco","signatures":[{"code":"new List([array])","description":"\n\nCreate an observable array-like object.\n","params":[{"types":[{"type":"Array","options":[]}],"optional":true,"name":"array","description":"Items to seed the List with.\n"}],"returns":{"types":[{"type":"can-list"}],"description":"An instance of `List` with the elements from _array_.\n"}},{"code":"new List(deferred)","description":"\n","params":[{"types":[{"type":"can.Deferred"}],"name":"deferred","description":"A deferred that resolves to an\narray. When the deferred resolves, its values will be added to the list.\n"}],"returns":{"types":[{"type":"can-list"}],"description":"An initially empty `List`. \n\n"}}],"_curReturn":{"types":[{"type":"can-list"}],"description":"An initially empty `List`. \n\n"},"_curParam":{"types":[{"type":"can.Deferred"}],"name":"deferred","description":"A deferred that resolves to an\narray. When the deferred resolves, its values will be added to the list.\n"},"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>