Skip to content

Commit

Permalink
update snoopy
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Hug committed Jan 23, 2016
1 parent e041abf commit cc62d6e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"globals"
],
"dependencies": {
"snoopy": "*"
"snoopy": "~0.1.0"
}
}
11 changes: 6 additions & 5 deletions bower_components/snoopy/.bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
"test",
"tests"
],
"_release": "e5afe8ac4e",
"version": "0.1.0",
"_release": "0.1.0",
"_resolution": {
"type": "branch",
"branch": "master",
"commit": "e5afe8ac4efc5a2a557d221f81e2ccb71c06d85b"
"type": "version",
"tag": "0.1.0",
"commit": "1c785f70d07118c38529b23765bb607dcabc31c4"
},
"_source": "git://github.com/Daniel-Hug/snoopy.git",
"_target": "*",
"_target": "~0.1.0",
"_originalSource": "snoopy",
"_direct": true
}
55 changes: 54 additions & 1 deletion bower_components/snoopy/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# snoopy
Observable objects in JS

## usage
## example

```js
// setup observable data
Expand All @@ -18,6 +18,59 @@ counter.snoop('even', function(even) {
});
```

## constructor and method usage

### `Snoopy()`

Snoopy is a constructor but the `new` keyword is optional. Call it passing an optional object with some initial properties and values:

```js
var counter = new Snoopy({count: 0});
```

### `Snoopy.prototype.set(property, value)`

```js
var person = Snoopy();
person.set('name', 'Hank');
```

### `Snoopy.prototype.get(property)`

There are two ways to get the value of a property on a Snoopy instance:

1. `person.get('name');`
2. `person.name`

### `Snoopy.prototype.snoop(property, mapFn)`

**arguments**

Here are the possible ways to call `.snoop()`:

```js
person.snoop('name')(function snooper(name) {
console.log('name is now ' + name);
})
```

```js
person.snoop('name', function snooper(name) {
console.log('name is now ' + name);
})
```

```js
person.snoop('firstName lastName', function map(firstName, lastName) {
return firstName + ' ' + lastName;
})(function snooper(name) {
console.log('name is now ' + name);
})
```

**return value**
When `.snoop()` is called, it returns a "snoopable" function. The snoopable function accepts a callback, calls it right away passing the values of the properties being snooped, and calls it again whenever one of the values change.

## 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: 18 additions & 8 deletions bower_components/snoopy/snoopy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Snoopy = (function() {
function Snoopy(obj) {
if (!(this instanceof Snoopy)) return new Snoopy(obj);
// don't overwrite this!:
// don't overwrite this.snoopers!
this.snoopers = {};
Object.keys(obj).forEach(function(key) {
this[key] = obj[key];
Expand All @@ -12,10 +12,10 @@ var Snoopy = (function() {
var newVal = this[prop];
(this.snoopers[prop] || []).forEach(function(snooper) {
snooper.call(this, newVal);
});
}, this);
};

var get = Snoopy.prototype.get = function(prop) {
Snoopy.prototype.get = function(prop, val) {
return this[prop];
};

Expand All @@ -24,14 +24,19 @@ var Snoopy = (function() {
changed.call(this, prop);
};

Snoopy.prototype.snoop = function(props, snooper) {
if (snooper) {
Snoopy.prototype.snoop = function(props, map) {
if (map) {
var snooper;
var mapOutput;
var propToVal = this.get.bind(this);
var propsArr = props.split(' ');

// caller calls snooper with the values of the passed props
var propToVal = get.bind(this);
// caller calls map with the values of the passed props
// then, if there is a snooper, calls that passing the value returned from the map
var caller = function caller() {
snooper.apply(null, propsArr.map(propToVal));
var propVals = propsArr.map(propToVal);
mapOutput = map.apply(null, propVals);
if (snooper) snooper(mapOutput);
};

caller();
Expand All @@ -42,6 +47,11 @@ var Snoopy = (function() {
if (!t.snoopers[prop]) t.snoopers[prop] = [];
t.snoopers[prop].push(caller);
});

return function mappedSnoop(newSnooper) {
snooper = newSnooper;
snooper(mapOutput);
};
} else {
// snoopable
return this.snoop.bind(this, props);
Expand Down

0 comments on commit cc62d6e

Please sign in to comment.