Code Monkey home page Code Monkey logo

attr's Introduction

component-attr

evented attributes with automatic dependencies

API

attr(val)

Create a new attr with value set to val

attr = require('attr')

name = attr('Homer Simpson')

xxx()

Read a value

name() // => 'Homer Simpson'

xxx(val)

Write a value

name('Bart Simpson')

name() // => 'Bart Simpson'
  • Emits "change" event with (value, previousValue).
name.on('change', function(new_name, old_name) {
  console.log('my name changed from', old_name, 'to', new_name)
})

Computed attr

Has the same API, except function is passed in and is run to determine the initial value. (has no setter)

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

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:

fullName = attr(function() {
  return firstName() + ' ' + surName()
}, [firstName])

fullName.dependencies // => [ firstName ]

attr methods

.toggle()

Flips truthy to false and falsey to true

.incr(val)

Increments value by 1 or val

.change(fn)

Binds fn to the change event

.change()

force a change event

.value

contains the current value. Use to get value without running the getter (most useful for computed attr)

.old

contains the last value

.dependencies

Contains a list of dependencies (this will be null for simple attr)

additional

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 attr - 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

make && open test/index.html

License

MIT

attr's People

Contributors

weepy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

gijs esundahl

attr's Issues

nested dependency checks

will probably fail

need to add a stack of dependencies in index.js rather than just a boolean flag

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.