Skip to content

Commit

Permalink
add documentation to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Hug committed Feb 1, 2016
1 parent cea7927 commit 24a3928
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
59 changes: 59 additions & 0 deletions README.md
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();
```
6 changes: 5 additions & 1 deletion dom-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// setup:
var observableArray = new ObservableArray([1,2,3,4,5,6,7]);
observableArray.addDomObserver(document.querySelector('ul'), function(num) {
var ul = document.querySelector('ul');
var observer = observableArray.addDomObserver(ul, function(num) {
var li = document.createElement('li');
li.textContent = 'number: ' + num;
return li;
Expand All @@ -12,6 +13,9 @@
// changes are reflected in DOM:
observableArray.push(8);
observableArray.splice(2);
// unsubscribe DOM observer:
observer.stop();
*/

ObservableArray.prototype.addDomObserver = (function() {
Expand Down

0 comments on commit 24a3928

Please sign in to comment.