define([
'libs/jquery',
+ 'libs/underscore',
'libs/backbone',
'smartxml/events'
-], function($, Backbone, events) {
+], function($, _, Backbone, events) {
'use strict';
nodeType: Node.ELEMENT_NODE,
setData: function(key, value) {
- this._$.data(key, value);
+ if(value !== undefined) {
+ this._$.data(key, value);
+ } else {
+ this._$.removeData(_.keys(this._$.data()));
+ this._$.data(key);
+ }
},
+
getData: function(key) {
- return this._$.data(key);
+ if(key) {
+ return this._$.data(key);
+ }
+ return this._$.data();
},
getTagName: function() {
'use strict';
/*jshint expr:true */
-/* global describe, it */
+/* global describe, it, beforeEach */
var expect = chai.expect;
expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
});
+
+ describe('Storing custom data', function() {
+ var node;
+
+ beforeEach(function() {
+ node = elementNodeFromXML('<div></div>');
+ });
+
+ it('can append single value', function() {
+ node.setData('key', 'value');
+ expect(node.getData('key')).to.equal('value');
+ });
+
+ it('can overwrite the whole data', function() {
+ node.setData('key1', 'value1');
+ node.setData({key2: 'value2'});
+ expect(node.getData('key2')).to.equal('value2');
+ });
+
+ it('can fetch the whole data at once', function() {
+ node.setData({key1: 'value1', key2: 'value2'});
+ expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
+ });
+ });
});
describe('Manipulations', function() {