Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(jqLite): support data() getter and data(obj) setter
Browse files Browse the repository at this point in the history
... just like jquery does
  • Loading branch information
IgorMinar committed May 4, 2012
1 parent 5df7e6f commit ee579a0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
16 changes: 15 additions & 1 deletion src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,21 @@ function JQLiteData(element, key, value) {
}
cache[key] = value;
} else {
return cache ? cache[key] : null;
if (isDefined(key)) {
if (isObject(key)) {
if (!cacheId) element[jqName] = cacheId = jqNextId();
jqCache[cacheId] = cache = (jqCache[cacheId] || {});
extend(cache, key);
} else {
return cache ? cache[key] : undefined;
}
} else {
if (!cacheId) element[jqName] = cacheId = jqNextId();

return cache
? cache
: cache = jqCache[cacheId] = {};
}
}
}

Expand Down
70 changes: 57 additions & 13 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,23 @@ describe('jqLite', function() {
it('should set and get and remove data', function() {
var selected = jqLite([a, b, c]);

expect(selected.data('prop', 'value')).toEqual(selected);
expect(selected.data('prop')).toEqual('value');
expect(jqLite(a).data('prop')).toEqual('value');
expect(jqLite(b).data('prop')).toEqual('value');
expect(jqLite(c).data('prop')).toEqual('value');
expect(selected.data('prop')).toBeUndefined();
expect(selected.data('prop', 'value')).toBe(selected);
expect(selected.data('prop')).toBe('value');
expect(jqLite(a).data('prop')).toBe('value');
expect(jqLite(b).data('prop')).toBe('value');
expect(jqLite(c).data('prop')).toBe('value');

jqLite(a).data('prop', 'new value');
expect(jqLite(a).data('prop')).toEqual('new value');
expect(selected.data('prop')).toEqual('new value');
expect(jqLite(b).data('prop')).toEqual('value');
expect(jqLite(c).data('prop')).toEqual('value');
expect(jqLite(a).data('prop')).toBe('new value');
expect(selected.data('prop')).toBe('new value');
expect(jqLite(b).data('prop')).toBe('value');
expect(jqLite(c).data('prop')).toBe('value');

expect(selected.removeData('prop')).toEqual(selected);
expect(jqLite(a).data('prop')).toEqual(undefined);
expect(jqLite(b).data('prop')).toEqual(undefined);
expect(jqLite(c).data('prop')).toEqual(undefined);
expect(selected.removeData('prop')).toBe(selected);
expect(jqLite(a).data('prop')).toBeUndefined();
expect(jqLite(b).data('prop')).toBeUndefined();
expect(jqLite(c).data('prop')).toBeUndefined();
});

it('should call $destroy function if element removed', function() {
Expand All @@ -247,6 +248,49 @@ describe('jqLite', function() {
element.remove();
expect(log).toEqual('destroy;');
});

it('should retrieve all data if called without params', function() {
var element = jqLite(a);
expect(element.data()).toEqual({});

element.data('foo', 'bar');
expect(element.data()).toEqual({foo: 'bar'});

element.data().baz = 'xxx';
expect(element.data()).toEqual({foo: 'bar', baz: 'xxx'});
});

it('should create a new data object if called without args', function() {
var element = jqLite(a),
data = element.data();

expect(data).toEqual({});
element.data('foo', 'bar');
expect(data).toEqual({foo: 'bar'});
});

it('should create a new data object if called with a single object arg', function() {
var element = jqLite(a),
newData = {foo: 'bar'};

element.data(newData);
expect(element.data()).toEqual({foo: 'bar'});
expect(element.data()).not.toBe(newData); // create a copy
});

it('should merge existing data object with a new one if called with a single object arg',
function() {
var element = jqLite(a);
element.data('existing', 'val');
expect(element.data()).toEqual({existing: 'val'});

var oldData = element.data(),
newData = {meLike: 'turtles', 'youLike': 'carrots'};

expect(element.data(newData)).toBe(element);
expect(element.data()).toEqual({meLike: 'turtles', youLike: 'carrots', existing: 'val'});
expect(element.data()).toBe(oldData); // merge into the old object
});
});


Expand Down

0 comments on commit ee579a0

Please sign in to comment.