Skip to content

Commit

Permalink
Merge branch 'live-binding' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Hug committed Jan 20, 2016
2 parents 0c94e03 + 2a3e397 commit bc7f28b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"moduleType": [
"amd",
"globals"
]
],
"dependencies": {
"snoopy": "*"
}
}
36 changes: 36 additions & 0 deletions bower_components/snoopy/.bower.json
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
}
23 changes: 23 additions & 0 deletions bower_components/snoopy/README.md
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.
26 changes: 26 additions & 0 deletions bower_components/snoopy/bower.json
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"
]
}
52 changes: 52 additions & 0 deletions bower_components/snoopy/snoopy.js
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;
})();
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>DOM Builder demo</title>
</head>
<body>
<script src="snoopy.js"></script>
<script src="bower_components/snoopy/snoopy.js"></script>
<script src="dom.js"></script>

<script>
Expand Down

0 comments on commit bc7f28b

Please sign in to comment.