Skip to content

Commit

Permalink
autocompute functiona arguments by default
Browse files Browse the repository at this point in the history
  • Loading branch information
weepy committed Dec 4, 2012
1 parent 967d758 commit 24636e3
Show file tree
Hide file tree
Showing 777 changed files with 120,729 additions and 16 deletions.
10 changes: 10 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0.0.2
=====

* Attr's with function arguments are assumed to be computed by default


0.0.1
=====

* Intial Release
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,10 @@ name.on('change', function(new_name, old_name) {

## Computed properties

These have the same API, except no setter and a function is passed in and is run to determine the initial value

### attr.computed(fn, dependencies)

Creates a computed attr.
Dependencies are automatically calculated unless set explicitly.
Has the same API, except function is passed in and is run to determine the initial value. (has no setter)

```javascript
var cattr = require('attr').computed

fullName = cattr(function() {
fullName = attr(function() {
return firstName() + ' ' + surName()
})

Expand All @@ -89,11 +82,27 @@ fullName() // => 'Homer Simpson'
fullName.dependencies // => [ firstName, surName ]
```

Dependencies on other simple (non-computed) are automatically calculated unless set explicitly via a second argument as in:

### attr.dependencies(fn)
```
fullName = attr(function() {
return firstName() + ' ' + surName()
}, [firstName])
Calculates a list of the simple attributes called by running this function
fullName.dependencies // => [ firstName ]
``
### attr.autocompute
By default function values are assumed to be computed. This behaviour can be turned off by setting autocompute.
### attr.computed(fn, dependencies)
Explicitly creates a computed property - this is only useful if autocompute is turned off
### attr.dependencies(fn)
Calculates a list of the simple (non-computed) attributes called by running this function
# Testing
Expand Down
3 changes: 1 addition & 2 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
"name": "attr",
"repo": "weepy/attr",
"description": "Simple property with get/set and events",
"version": "0.0.1",
"version": "0.0.2",
"keywords": [],
"dependencies": {
"component/assert": "*",
"component/emitter": "*"
},
"development": {
Expand Down
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ function extend(a, b) {

var watcher = false

function Attr(arg) {
function Attr(arg, dependencies) {

// IDEA: autocreate computed attr for function values
// if(typeof arg =='function') return Attr.computed(arg)
// autocreate computed attr for function values
if(Attr.autocompute && typeof arg =='function') return Attr.computed(arg, dependencies)

function attr(v) {
if(arguments.length) {
Expand All @@ -61,6 +61,12 @@ function Attr(arg) {
return attr
}

/*
* Autocompute functions
*/

Attr.autocompute = true


var lastValue = null

Expand All @@ -76,6 +82,8 @@ Attr.dependencies = function(fn) {
}




/*
* computed attribute
*/
Expand Down Expand Up @@ -111,6 +119,8 @@ Attr.computed = function(fn, dependencies) {
attr.change()
}

attr.computed = true

return attr
}

Expand Down
1 change: 1 addition & 0 deletions node_modules/.bin/component

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/component-build

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/component-changes

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/component-convert

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/component-create

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/component-docs

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/component-help

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/component-info

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/component-install

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/component-ls

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/component-open

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/component-search

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/component-wiki

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/component/.npmignore

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

170 changes: 170 additions & 0 deletions node_modules/component/History.md

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

13 changes: 13 additions & 0 deletions node_modules/component/Makefile

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

Loading

0 comments on commit 24636e3

Please sign in to comment.