-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cea7927
commit 24a3928
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,61 @@ | ||
# observable-array | ||
lightweight JS observable array constructor with methods cloned from Array.prototype and changed to notify observers after completion | ||
|
||
|
||
## usage | ||
|
||
### create an observable array (requires [observable-array.js](observable-array.js)) | ||
|
||
```js | ||
var observableArray = new ObservableArray([1,2,3,4,5,6,7]); | ||
``` | ||
|
||
`ObservableArray` comes with all the native array methods you're used to: `push`, `slice`, `shift`, `forEach`, `map`, etc.: | ||
|
||
```js | ||
// these do what you'd expect but also notify observers | ||
observableArray.push(8); | ||
observableArray.splice(2); | ||
``` | ||
|
||
|
||
### observe changes (uses method sniffing) | ||
|
||
The following methods for observing the arrays (`.on()` and `.off()`) come from [subscribable.js](https://github.com/Daniel-Hug/subscribable.js) (a dependency of observable-array.js): | ||
|
||
```js | ||
observableArray.on('push', function(methodName, newItem1, newItem2/*, ...*/) { | ||
// new item(s) appended | ||
}); | ||
``` | ||
|
||
```js | ||
function handlePop() { | ||
// last item removed | ||
} | ||
|
||
// get notified when observableArray.pop() is called | ||
observableArray.on('pop', handlePop); | ||
|
||
// unsubscribe observable | ||
observableArray.off('pop', handlePop); | ||
``` | ||
|
||
|
||
### add dom observers (requires [dom-observer.js](dom-observer.js)) | ||
|
||
```js | ||
var ul = document.querySelector('ul'); | ||
|
||
function itemRenderer(num) { | ||
var li = document.createElement('li'); | ||
li.textContent = 'number: ' + num; | ||
return li; | ||
} | ||
|
||
// add DOM observer: | ||
var observer = observableArray.addDomObserver(ul, itemRenderer); | ||
|
||
// unsubscribe DOM observer: | ||
observer.stop(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters