Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
attach data to elements. think $.data()
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
History.md
Makefile
Readme.md
component.json
index.js

Readme.md

data

attach data to elements. think $.data().

Installation

$ component install yields/data

API

api = data(el)

Get new data api for the given element.

api.set(key[, val])

Merge the provided obj or set key to val.

data(el).set({ foo: 'bar' })
data(el).set({ bar: 'foo' });
data(el).get();
// > { foo: 'bar', bar: 'foo' }

api.get([key])

Get all data or a single value by key, if the key does not exists it will be looked-up in the element data-* attributes, when found it will be cached and then returned.

data(el).get('foo');
// > null
data(el).get();
// > {}

api.has(key)

Whether or not key exists

data(el).set('foo', 'bar').has('foo');
// > true

api.del([key])

Delete all data or a single key from cache.

data(el).del('some key');
data(el).del(); // everything!

api.cache

the element cache

api.el

the element.

add custom methods.

data.api.getUserAge = function () {
  return this.cache.user
    && this.cache.user.age;
};

data(el).set({ user: { age: 99 } }).getUserAge();
// > 99

License

MIT

Something went wrong with that request. Please try again.