Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
weepy committed Nov 13, 2012
1 parent 306444a commit d3adfe9
Show file tree
Hide file tree
Showing 220 changed files with 46,560 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
components
build
Empty file added History.md
Empty file.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

build: components index.js
@component build --dev

components: component.json
@component install --dev

clean:
rm -fr build components template.js

test:
@./node_modules/.bin/mocha \
--reporter spec \
--require should

.PHONY: clean test
18 changes: 18 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "attr",
"repo": "weepy/attr",
"description": "Simple property with get/set and events",
"version": "0.0.1",
"keywords": [],
"dependencies": {
"component/assert": "*",
"component/emitter": "*"
},
"development": {
"component/assert": "*"
},
"license": "MIT",
"scripts": [
"index.js"
]
}
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
var Emitter = require('emitter')

var methods = {
incr: function (val) {
return this(this.value + (val || 1))
},
toggle: function (val) {
return this(!this.value)
},
change: function(fn) {
if(fn) {
this.on('change', fn) // setup observer
}
else { // force the change
var val = this()
this.emit('change', val, this.old)
}

return this
},
toString: function() {
return "attr:"+JSON.stringify(this.value)
}
}

Emitter(methods)


function createAttr(arg) {
function attr(v) {
if(arguments.length) {
attr.old = attr.value
attr.value = v
attr.emit('change', attr.value, attr.old)
}
return attr.value
}


for(var i in methods) attr[i] = methods[i]
// set to initial
attr.value = arg


return attr
}

createAttr.computed = function(fn) {
function cattr() {
cattr.old = cattr.value
cattr.value = fn()
cattr.emit('change', cattr.value, cattr.old)
return cattr.value
}


for(var i in methods) cattr[i] = methods[i]

cattr.value = fn()

// setup dependencies
cattr._depends = []


cattr.depends = function(deps) {
cattr._depends.forEach(function(dep) {
dep.off('change', changeFn)
})

cattr._depends = deps
deps.forEach(function(dep) {
dep.on('change', changeFn)
})
return cattr
}

function changeFn() {
cattr.change()
}

return cattr
}

module.exports = createAttr
1 change: 1 addition & 0 deletions node_modules/.bin/_mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions node_modules/mocha/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/mocha/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d3adfe9

Please sign in to comment.