-
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.
- Loading branch information
0 parents
commit f578612
Showing
15 changed files
with
1,055 additions
and
0 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
service_name: travis-ci |
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,2 @@ | ||
/node_modules | ||
/test/reports |
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,6 @@ | ||
verbose: false | ||
reporting: | ||
print: summary | ||
reports: | ||
- lcov | ||
dir: ./test/reports/coverage |
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 @@ | ||
/test/reports |
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,6 @@ | ||
language: node_js | ||
node_js: | ||
- "0.11" | ||
- "0.10" | ||
|
||
script: make test-ci |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 David Rekow. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,69 @@ | ||
# panoptic makefile | ||
# author: David Rekow <d@davidrekow.com> | ||
# copyright: David Rekow 2014 | ||
|
||
|
||
SHELL := /bin/bash | ||
|
||
# vars | ||
THIS_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) | ||
|
||
.PHONY: build clean distclean test test-ci | ||
|
||
all: build | ||
|
||
build: test | ||
@echo "Building panoptic..." | ||
@mkdir -p $(THIS_DIR)/dist | ||
|
||
@echo "Compiling dist package with closure compiler..." | ||
@-java -jar $(THIS_DIR)/node_modules/closure-compiler-stream/lib/compiler.jar \ | ||
--debug false \ | ||
--warning_level QUIET \ | ||
--summary_detail_level 1 \ | ||
--js $(THIS_DIR)/index.js \ | ||
--language_in ECMASCRIPT5 \ | ||
--formatting PRETTY_PRINT \ | ||
--compilation_level WHITESPACE_ONLY \ | ||
--js_output_file $(THIS_DIR)/dist/panoptic.js \ | ||
--common_js_entry_module $(THIS_DIR)/index.js \ | ||
--output_wrapper '(function () {%output%}).call(this);' | ||
|
||
@echo "Minifying dist package with closure compiler..." | ||
@-java -jar $(THIS_DIR)/node_modules/closure-compiler-stream/lib/compiler.jar \ | ||
--debug false \ | ||
--warning_level QUIET \ | ||
--summary_detail_level 1 \ | ||
--js $(THIS_DIR)/index.js \ | ||
--language_in ECMASCRIPT5 \ | ||
--compilation_level ADVANCED_OPTIMIZATIONS \ | ||
--common_js_entry_module $(THIS_DIR)/index.js \ | ||
--js_output_file $(THIS_DIR)/dist/panoptic.min.js \ | ||
--output_wrapper '(function () {%output%}).call(this);' \ | ||
--use_types_for_optimization | ||
|
||
@echo "Build complete." | ||
|
||
clean: | ||
@echo "Cleaning built files..." | ||
@-rm -rf $(THIS_DIR)/dist | ||
|
||
@echo "Cleaning test reports..." | ||
@-rm -rf $(THIS_DIR)/test/reports | ||
|
||
distclean: clean | ||
@echo "Cleaning downloaded dependencies..." | ||
@-rm -rf $(THIS_DIR)/node_modules | ||
|
||
test: $(THIS_DIR)/node_modules | ||
@echo "Running panoptic package tests..." | ||
@multi="xunit=test/reports/xunit.xml spec=-" \ | ||
$(THIS_DIR)/node_modules/.bin/istanbul cover $(THIS_DIR)/node_modules/.bin/_mocha -- -R mocha-multi | ||
|
||
test-ci: test | ||
@echo "Reporting coverage to coveralls..." | ||
@cat $(THIS_DIR)/test/reports/coverage/lcov.info | $(THIS_DIR)/node_modules/.bin/coveralls | ||
|
||
$(THIS_DIR)/node_modules: | ||
@echo "Installing NPM build dependencies..." | ||
@npm install |
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,89 @@ | ||
#panoptic | ||
simple object keypath observers. | ||
|
||
[![Build Status](https://travis-ci.org/davidrekow/panoptic.svg?branch=master)](https://travis-ci.org/davidrekow/panoptic) [![Coverage Status](https://coveralls.io/repos/davidrekow/panoptic/badge.png?branch=master)](https://coveralls.io/r/davidrekow/panoptic?branch=master) | ||
|
||
##Installation | ||
###node | ||
Add to your `dependencies` in `package.json`: | ||
```javascript | ||
... | ||
"dependencies": { | ||
"panoptic": "~0.0.1", | ||
... | ||
}, | ||
... | ||
``` | ||
or install directly: | ||
```sh | ||
npm install --save panoptic | ||
``` | ||
###browser | ||
Include either of the bundles in the `dist/` folder of the [repo](https://github.com/davidrekow/panoptic), | ||
depending on whether or not you'd like it minified. | ||
|
||
##Usage | ||
Import the `panoptic` module: | ||
```javascript | ||
var panoptic = require('panoptic'); | ||
``` | ||
then call with an object you want to observe: | ||
```javascript | ||
var data = { | ||
name: 'Cool Person', | ||
age: 25 | ||
}; | ||
|
||
var observable = panoptic(data); | ||
``` | ||
###getting | ||
```javascript | ||
value = observable.get('key'); | ||
value = observable.key; | ||
``` | ||
Retrieve the value for a particular key via getter or object property access. | ||
If you're not sure whether the key exists before accessing, use `get()` to | ||
avoid a TypeError when retrieving deeply nested values: | ||
```javascript | ||
value = observable.get('a.b.c'); // if a or b doesn't exist, returns null | ||
value = observable.a.b.c; // if a or b doesn't exist, throws TypeError | ||
``` | ||
###setting | ||
```javascript | ||
observable.set('key', value); | ||
observable.key = value; | ||
``` | ||
Set the value of a key in the same way. If setting a deeply nested key and you | ||
don't know whether intermediate objects exist, use `set()` and they will be | ||
created (think `mkdir -p`): | ||
```javascript | ||
observable.set('a.b.c', value); // if a or b doesn't exist they will be created | ||
observable.a.b.c = value; // if a or b doesn't exist, throws TypeError | ||
``` | ||
###watching | ||
```javascript | ||
observable.watch('a.b.c', function (newValue) { | ||
var value = this.get('c'); // 'this' is the object the value is retrieved from | ||
value === newValue; // watcher receives the new value after it's set | ||
}); | ||
``` | ||
###unwatching | ||
```javascript | ||
var watcher = function (newValue) { ... }; | ||
|
||
observable.watch('key', watcher); | ||
observable.unwatch('key', watcher); // if watcher is passed, only it gets removed | ||
observable.unwatch('key'); // if no watcher is passed, all get removed | ||
``` | ||
That's it! | ||
|
||
##FAQ | ||
###why panoptic? | ||
it's super lightweight, matches whatever data access syntax you're currently | ||
using, and uses a simple but powerful sparse-tree data structure to avoid the | ||
overhead of propagation and digest cycles when dispatching change events. | ||
|
||
###why `panoptic`? | ||
panopticon (the less morbid connotations), also *pan* (all) + *opt* (option?) | ||
|
||
Find a bug? Please [file an issue](https://github.com/davidrekow/panoptic/issues)! |
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,38 @@ | ||
{ | ||
"name": "panoptic", | ||
"description": "see all, do all. simple object keypath observers.", | ||
"version": "0.0.1", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "David Rekow", | ||
"email": "d@davidrekow.com", | ||
"url": "http://davidrekow.com" | ||
}], | ||
"keywords": [ | ||
"panoptic", | ||
"observe", | ||
"observer", | ||
"observers", | ||
"observe object", | ||
"watch", | ||
"watcher", | ||
"watchers", | ||
"watch object", | ||
"watch property", | ||
"data", | ||
"bind", | ||
"databind", | ||
"data bind", | ||
"databinding", | ||
"data binding", | ||
"property binding", | ||
"bind data", | ||
"bind property" | ||
], | ||
"homepage": "https://github.com/davidrekow/panoptic", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/davidrekow/panoptic.git" | ||
}, | ||
"main": "index.js" | ||
} |
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,106 @@ | ||
(function () {/* | ||
David Rekow 2014 | ||
*/ | ||
function Observable(data, root, path) { | ||
var k; | ||
if (root) { | ||
Observable.setRoot(this, root, path); | ||
} | ||
this._prop = {}; | ||
this._cb = {}; | ||
for (k in data) { | ||
if (data.hasOwnProperty(k) && typeof data[k] !== "function") { | ||
this.bind(k, data[k]); | ||
} | ||
} | ||
} | ||
Observable.prototype = {get:function(key) { | ||
return Observable.resolve(this, key); | ||
}, set:function(key, value) { | ||
Observable.resolve(this, key, value); | ||
}, watch:function(key, observer) { | ||
if (this._root) { | ||
return this._root.watch(this._path + "." + key, observer); | ||
} | ||
if (!this._cb[key]) { | ||
this._cb[key] = []; | ||
} | ||
this._cb[key].push(observer); | ||
}, unwatch:function(key, observer) { | ||
var i; | ||
if (this._root) { | ||
return this._root.unwatch(this._path + "." + key, observer); | ||
} | ||
if (!this._cb[key]) { | ||
return; | ||
} | ||
if (!observer) { | ||
this._cb[key] = []; | ||
return; | ||
} | ||
i = this._cb[key].indexOf(observer); | ||
if (i > -1) { | ||
this._cb[key].splice(i, 1); | ||
} | ||
}, toJSON:function() { | ||
return this._prop; | ||
}, _root:null, _path:null, constructor:Observable, emit:function(key, value, observed) { | ||
var observers; | ||
if (this._root) { | ||
return this._root.emit(this._path + "." + key, value, observed); | ||
} | ||
observers = this._cb[key]; | ||
if (!observers) { | ||
return; | ||
} | ||
observers.forEach(function(observer) { | ||
observer.call(observed, value); | ||
}); | ||
}, bind:function(key, value) { | ||
Object.defineProperty(this, key, {enumerable:true, get:function() { | ||
return this._prop[key]; | ||
}, set:function(value) { | ||
this._prop[key] = value; | ||
this.emit(key, value, this); | ||
}}); | ||
this.set(key, value); | ||
}}; | ||
Observable.resolve = function(observed, key, value) { | ||
var path = key.split("."), pathname = path.pop(), fullpath = observed._path || "", root = observed._root || observed, _path, i; | ||
for (i = 0;i < path.length;i++) { | ||
_path = path[i]; | ||
fullpath += "." + _path; | ||
if (!observed.hasOwnProperty(_path)) { | ||
if (value === undefined || value === null) { | ||
return null; | ||
} | ||
observed.bind(_path, new Observable(null, root, fullpath)); | ||
} | ||
observed = observed[_path]; | ||
} | ||
fullpath += fullpath ? "." + pathname : pathname; | ||
if (value === undefined) { | ||
return observed[pathname]; | ||
} | ||
if (typeof value === "object" && !(value instanceof Observable || value instanceof Array)) { | ||
value = new Observable(value, root, fullpath); | ||
} | ||
if (!observed.hasOwnProperty(pathname)) { | ||
observed.bind(pathname); | ||
} | ||
observed[pathname] = value; | ||
}; | ||
Observable.setRoot = function(observed, root, path) { | ||
Object.defineProperty(observed, "_root", {value:root}); | ||
Object.defineProperty(observed, "_path", {value:path}); | ||
}; | ||
this.panoptic = function(data) { | ||
return new Observable(data); | ||
}; | ||
if (typeof module !== "undefined" && typeof module["exports"] === "object") { | ||
module.exports = this.panoptic; | ||
} | ||
if (typeof define === "function") { | ||
define("panoptic", this.panoptic); | ||
} | ||
;}).call(this); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.