neft
Version:
Universal Platform
258 lines (178 loc) • 10.6 kB
Markdown
> [Wiki](Home) ▸ [[API Reference|API-Reference]] ▸ **List**
# List
> data-binding on arrays
Access it with:
```javascript
const { List } = Neft;
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee)
## Table of contents
* [List](#list)
* [**Class** List](#class-list)
* [constructor](#constructor)
* [onChange](#onchange)
* [onInsert](#oninsert)
* [onPop](#onpop)
* [length](#length)
* [set](#set)
* [append](#append)
* [insert](#insert)
* [extend](#extend)
* [remove](#remove)
* [pop](#pop)
* [clear](#clear)
* [index](#index)
* [has](#has)
* [toArray](#toarray)
* [Glossary](#glossary)
# **Class** List
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee)
##constructor
<dl><dt>Syntax</dt><dd><code>List::constructor([*Array* data])</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>data — <i>Array</i> — <i>optional</i></li></ul></dd></dl>
Creates a new list instance.
```javascript
var list = new List([1, 2]);
list instanceof List; // true
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#listconstructorarray-data)
##onChange
<dl><dt>Syntax</dt><dd><code>*Signal* List::onChange(*Any* oldValue, *Integer* index)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>oldValue — <i>Any</i></li><li>index — <a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></li></ul></dd><dt>Type</dt><dd><a href="/Neft-io/neft/wiki/Signal-API#class-signal">Signal</a></dd></dl>
Signal called on each value change.
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#signal-listonchangeany-oldvalue-integer-index)
##onInsert
<dl><dt>Syntax</dt><dd><code>*Signal* List::onInsert(*Any* value, *Integer* index)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>value — <i>Any</i></li><li>index — <a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></li></ul></dd><dt>Type</dt><dd><a href="/Neft-io/neft/wiki/Signal-API#class-signal">Signal</a></dd></dl>
Signal called when a value was added.
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#signal-listoninsertany-value-integer-index)
##onPop
<dl><dt>Syntax</dt><dd><code>*Signal* List::onPop(*Any* oldValue, *Integer* index)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>oldValue — <i>Any</i></li><li>index — <a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></li></ul></dd><dt>Type</dt><dd><a href="/Neft-io/neft/wiki/Signal-API#class-signal">Signal</a></dd></dl>
Signal called when a value was removed.
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#signal-listonpopany-oldvalue-integer-index)
##length
<dl><dt>Syntax</dt><dd><code>ReadOnly *Integer* List::length</code></dd><dt>Prototype property of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Type</dt><dd><a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></dd><dt>Read Only</dt></dl>
Amount of values stored in the list.
```javascript
var list = new List(['a', 'b']);
list.length; // 2
```
##set
<dl><dt>Syntax</dt><dd><code>*Any* List::set(*Integer* index, *Any* value)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>index — <a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></li><li>value — <i>Any</i></li></ul></dd><dt>Returns</dt><dd><i>Any</i></dd></dl>
Sets the given value under the given index.
The index must exists in the list.
The value can't be an `undefined`. Use `pop()` instead.
Calls `onChange()` signal.
```javascript
var types = new List(['fantasy', 'Thriller']);
types.onChange.connect(function(oldVal, i){
console.log("element "+oldVal+" changed to "+this[i]);
});
types.set(0, 'Fantasy');
// element fantasy changed to Fantasy
types.set(0, 'Fantasy');
// nothing changed ...
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#any-listsetinteger-index-any-value)
##append
<dl><dt>Syntax</dt><dd><code>*Any* List::append(*Any* value)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>value — <i>Any</i></li></ul></dd><dt>Returns</dt><dd><i>Any</i></dd></dl>
Adds the given value on the end on the list.
The value can't be an `undefined`.
Calls `onInsert()` signal.
```javascript
var fridge = new List(['apple', 'milk']);
fridge.onInsert.connect(function(val, i){
console.log(val+" appended!");
});
fridge.append('cheese');
// cheese appended!
console.log(fridge.items());
// apple, milk, cheese
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#any-listappendany-value)
##insert
<dl><dt>Syntax</dt><dd><code>*Any* List::insert(*Integer* index, *Any* value)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>index — <a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></li><li>value — <i>Any</i></li></ul></dd><dt>Returns</dt><dd><i>Any</i></dd></dl>
Add the given value under the given index.
The index can't be greater than the list length.
The value can't be an `undefined`.
Calls `onInsert()` signal.
```javascript
var list = new List(['a', 'b']);
list.onInsert.connect(function(val, i){
console.log("new element "+val+" inserted at index "+i);
});
list.insert(1, 'c');
// new element c inserted at index 1
console.log(list.items());
// ['a', 'c', 'b']
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#any-listinsertinteger-index-any-value)
##extend
<dl><dt>Syntax</dt><dd><code>*List* List::extend(*Object* items)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>items — <a href="/Neft-io/neft/wiki/Utils-API#isobject">Object</a></li></ul></dd><dt>Returns</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd></dl>
Appends all values stored in the given items into the list.
Calls `onInsert()` signal for each value.
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#list-listextendobject-items)
##remove
<dl><dt>Syntax</dt><dd><code>*Any* List::remove(*Any* value)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>value — <i>Any</i></li></ul></dd><dt>Returns</dt><dd><i>Any</i></dd></dl>
Removes the given value from the list.
Calls `onPop()` signal.
```javascript
var list = new List(['a', 'b']);
list[1]; // b
list.remove('b');
list[1]; // undefined
list; // ['a']
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#any-listremoveany-value)
##pop
<dl><dt>Syntax</dt><dd><code>*Any* List::pop([*Integer* index])</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>index — <a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a> — <i>optional</i></li></ul></dd><dt>Returns</dt><dd><i>Any</i></dd></dl>
Removes the value stored in the list under the given index.
The index must exists in the list.
Calls `onPop()` signal.
```javascript
var list = new List(['a', 'b']);
list[1]; // b
list.pop(1);
list[1]; // undefined
list; // ['a']
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#any-listpopinteger-index)
##clear
<dl><dt>Syntax</dt><dd><code>List::clear()</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd></dl>
Removes all values stored in the list.
Calls `onPop()` signal for each value.
```javascript
var list = new List(['a', 'b']);
list.onPop.connect(function(oldVal, i){
console.log("Element "+oldVal+" popped!");
});
console.log(list.items());
// ['a', 'b']
list.clear()
// Element b popped!
// Element a popped!
console.log(list);
// []
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#listclear)
##index
<dl><dt>Syntax</dt><dd><code>*Integer* List::index(*Any* value)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>value — <i>Any</i></li></ul></dd><dt>Returns</dt><dd><a href="/Neft-io/neft/wiki/Utils-API#isinteger">Integer</a></dd></dl>
Returns the given value index in the list.
Returns `-1` if the value doesn't exist in the list.
```javascript
var list = new List(['a', 'b']);
list.index('b'); // 1
list.index('c'); // -1
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#integer-listindexany-value)
##has
<dl><dt>Syntax</dt><dd><code>*Boolean* List::has(*Any* value)</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Parameters</dt><dd><ul><li>value — <i>Any</i></li></ul></dd><dt>Returns</dt><dd><i>Boolean</i></dd></dl>
Returns `true` if the given value exists in the list.
```javascript
var list = new List(['a', 'b']);
list.has('a'); // true
list.has('ab123'); // false
```
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#boolean-listhasany-value)
##toArray
<dl><dt>Syntax</dt><dd><code>*Array* List::toArray()</code></dd><dt>Prototype method of</dt><dd><a href="/Neft-io/neft/wiki/List-API#class-list">List</a></dd><dt>Returns</dt><dd><i>Array</i></dd></dl>
> [`Source`](/Neft-io/neft/blob/e79ebc2b61607e795a53c22d1577605addf00689/src/list/index.litcoffee#array-listtoarray)
# Glossary
- [List](#class-list)