https://github.com/tenphi/array-model
Extension of native Array object to make it a collection model
https://github.com/tenphi/array-model
Last synced: 24 days ago
JSON representation
Extension of native Array object to make it a collection model
- Host: GitHub
- URL: https://github.com/tenphi/array-model
- Owner: tenphi
- Created: 2013-09-14T20:42:57.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-09-15T05:53:32.000Z (over 12 years ago)
- Last Synced: 2025-08-20T10:55:48.477Z (10 months ago)
- Language: CoffeeScript
- Size: 102 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Array-Model [](https://travis-ci.org/tenphi/array-model)
===
Extension of native Array object to make it a collection model. But it still real Array. So you can just wrap any exist Array in your project.
## Installation
via npm
```bash
npm install array-model
```
## Usage
Just create a array, call method `.model()` and you will have great collection object.
Simple example prevents addition of objects with wrong type.
```javascript
var arr = [].model();
arr.on('add', function(val, pos, arr) {
if (typeof val !== 'number')
arr.splice(pos, 1);
});
arr.push(42);
console.log(arr.slice());
arr.push('24');
console.log(arr.slice());
arr.push(24);
console.log(arr.slice());
console.log(Array.isArray(arr))
// [42]
// [42]
// [42, 24]
// true
```
Events work great even with assignments.
```javascript
var arr = [1,2,3].model()
arr.on('add', function(val, pos) {
console.log(val, pos);
});
arr.on('remove', function(val, pos) {
console.log(val, pos);
});
arr[1] = 4
// 2 1
// 4 1
```
Array-Model provides useful getters for you!
```javascript
var arr = [1,2,3].model();
arr.get(function(val) {
return val*val;
});
console.log(arr.slice());
console.log(arr[2]);
// [1,4,9]
// 9
```
See more examples in `spec.coffee`.
## What Array-Model cannot do for you
Assignment to not exist values. It will not work and probably break your object.
```javascript
var arr = [].model();
arr.on('add', function(val, pos) { // won't be fired
console.log(val, pos);
});
arr[0] = 'data';
```