-
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.
Merge branch 'live-binding' into gh-pages
- Loading branch information
Showing
6 changed files
with
142 additions
and
2 deletions.
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 |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
"moduleType": [ | ||
"amd", | ||
"globals" | ||
] | ||
], | ||
"dependencies": { | ||
"snoopy": "*" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "snoopy", | ||
"main": "snoopy.js", | ||
"homepage": "https://github.com/Daniel-Hug/snoopy", | ||
"authors": [ | ||
"Daniel Hug <[email protected]>" | ||
], | ||
"description": "Observable objects in JS", | ||
"moduleType": [ | ||
"globals" | ||
], | ||
"keywords": [ | ||
"Observable", | ||
"watch", | ||
"subscribe", | ||
"observe" | ||
], | ||
"license": "MIT", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
], | ||
"_release": "e5afe8ac4e", | ||
"_resolution": { | ||
"type": "branch", | ||
"branch": "master", | ||
"commit": "e5afe8ac4efc5a2a557d221f81e2ccb71c06d85b" | ||
}, | ||
"_source": "git://github.com/Daniel-Hug/snoopy.git", | ||
"_target": "*", | ||
"_originalSource": "snoopy", | ||
"_direct": true | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# snoopy | ||
Observable objects in JS | ||
|
||
## usage | ||
|
||
```js | ||
// setup observable data | ||
var counter = new Snoopy({count: 0}); | ||
|
||
// counter.even subscribes to counter.count | ||
counter.snoop('count', function(val) { | ||
counter.set('even', val % 2 === 0); | ||
}); | ||
|
||
// log: "3 is odd." or "0 is even." | ||
counter.snoop('even', function(even) { | ||
console.log(this.counter + ' is ' + (even ? 'even' : 'odd') + '.'); | ||
}); | ||
``` | ||
|
||
## use with [DOM Builder](https://github.com/Daniel-Hug/DOM-Builder) | ||
|
||
This Observable module was made with DOM Builder in mind and the two form a very powerful duo. |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "snoopy", | ||
"main": "snoopy.js", | ||
"homepage": "https://github.com/Daniel-Hug/snoopy", | ||
"authors": [ | ||
"Daniel Hug <[email protected]>" | ||
], | ||
"description": "Observable objects in JS", | ||
"moduleType": [ | ||
"globals" | ||
], | ||
"keywords": [ | ||
"Observable", | ||
"watch", | ||
"subscribe", | ||
"observe" | ||
], | ||
"license": "MIT", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var Snoopy = (function() { | ||
function Snoopy(obj) { | ||
if (!(this instanceof Snoopy)) return new Snoopy(obj); | ||
// don't overwrite this!: | ||
this.snoopers = {}; | ||
Object.keys(obj).forEach(function(key) { | ||
this[key] = obj[key]; | ||
}, this); | ||
} | ||
|
||
var changed = Snoopy.prototype.changed = function(prop) { | ||
var newVal = this[prop]; | ||
(this.snoopers[prop] || []).forEach(function(snooper) { | ||
snooper.call(this, newVal); | ||
}); | ||
}; | ||
|
||
var get = Snoopy.prototype.get = function(prop) { | ||
return this[prop]; | ||
}; | ||
|
||
Snoopy.prototype.set = function(prop, val) { | ||
this[prop] = val; | ||
changed.call(this, prop); | ||
}; | ||
|
||
Snoopy.prototype.snoop = function(props, snooper) { | ||
if (snooper) { | ||
var propsArr = props.split(' '); | ||
|
||
// caller calls snooper with the values of the passed props | ||
var propToVal = get.bind(this); | ||
var caller = function caller() { | ||
snooper.apply(null, propsArr.map(propToVal)); | ||
}; | ||
|
||
caller(); | ||
|
||
// push caller to snooper list | ||
var t = this; | ||
propsArr.forEach(function(prop) { | ||
if (!t.snoopers[prop]) t.snoopers[prop] = []; | ||
t.snoopers[prop].push(caller); | ||
}); | ||
} else { | ||
// snoopable | ||
return this.snoop.bind(this, props); | ||
} | ||
}; | ||
|
||
return Snoopy; | ||
})(); |
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