Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anthonyshort/observable
Get and set attributes and watch for changes. The simplest type of model there is.
https://github.com/anthonyshort/observable
Last synced: 24 days ago
JSON representation
Get and set attributes and watch for changes. The simplest type of model there is.
- Host: GitHub
- URL: https://github.com/anthonyshort/observable
- Owner: anthonyshort
- Created: 2013-08-23T13:06:34.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2013-10-03T01:16:24.000Z (about 11 years ago)
- Last Synced: 2024-09-14T13:40:08.898Z (about 2 months ago)
- Language: JavaScript
- Size: 148 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
observable
==========Get and set attributes and watch for changes. The simplest type of model there is. Will work
with Reactive and emits both Backbone-style and Reactive-style events ('change:foo' vs 'change foo').## Installation
Component:
component install anthonyshort/observable
npm:
npm install observable-component
## Usage
```js
var Observable = require('observable');
var model = new Observable();model.on('change color', function(val, previous){
console.log('Color was ' + previous + ', now it is ' + val);
});model.set('color', 'red'); // Color was undefined, now it is red
model.set('color', 'blue'); // Color was red, now it is blue// Set many at once
model.set({
color: 'green',
background: 'lightGreen'
});// Set attributes silently
model.set('color', 'red', { silent: true });// Get the attribute
model.get('color') // red
```You can mix it in too:
```js
function Thingy(){
this.set('color', 'red');
}Observable(Thingy.prototype);
var thing = new Thingy();
thing.get('color') // 'red'
```Create attributes that work with Reactive:
```js
var model = new Observable();model.attr('name')
.attr('surname')
.attr('title');
model.name('Barry');
model.name(); // Barry
```Access deep object properties using [tea-properties](https://github.com/qualiancy/tea-properties):
```js
var model = new Observable();model.on('change foo.bar.baz', function(val){
console.log(val);
});model.set('foo.bar.baz', 'doobie');
model.get('foo.bar.baz'); // 'doobie'
```