diff --git a/src/item/Item.js b/src/item/Item.js index 8c3ed016..32de56d6 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -602,7 +602,51 @@ var Item = this.Item = Base.extend(Callback, { // TODO: get/setIsolated (print specific feature) // TODO: get/setKnockout (print specific feature) // TODO: get/setAlphaIsShape - // TODO: get/setData + + /** + * A plain javascript object which can be used to store + * arbitrary data on the item. + * + * @type Object + * @bean + * + * @example + * var path = new Path(); + * path.data.remember = 'milk'; + * + * @example + * var path = new Path(); + * path.data.malcolm = new Point(20, 30); + * console.log(path.data.malcolm.x); // 20 + * + * @example + * var path = new Path(); + * path.data = { + * home: 'Omicron Theta', + * found: 2338, + * pets: ['Spot'] + * }; + * console.log(path.data.pets.length); // 1 + * + * @example + * var path = new Path({ + * data: { + * home: 'Omicron Theta', + * found: 2338, + * pets: ['Spot'] + * } + * }); + * console.log(path.data.pets.length); // 1 + */ + getData: function() { + if (!this._data) + this._data = {}; + return this._data; + }, + + setData: function(data) { + this._data = data; + }, /** * {@grouptitle Position and Bounding Boxes} diff --git a/test/tests/Item.js b/test/tests/Item.js index bce28690..4aa462fa 100644 --- a/test/tests/Item.js +++ b/test/tests/Item.js @@ -556,4 +556,34 @@ test('Item#isInserted', function() { equals(item.isInserted(), true); group.remove(); equals(item.isInserted(), false); +}); + +test('Item#data', function() { + var item = new Path(); + var description = 'When accessed before any data was set, a plain object is created for us'; + equals(Base.isPlainObject(item.data), true, description); + + var item = new Path(); + item.data.test = true; + equals(item.data.test, true, description); + + var item = new Path(); + var point = new Point(); + item.data.point = point; + equals(item.data.point, point, 'We can set basic types on data'); + + var item = new Path(); + item.data = { + testing: true + } + equals(item.data.testing, true, 'we can set data using an object literal'); + + var item = new Path({ + data: { + testing: true + } + }); + equals(item.data.testing, true, 'we can set data using an object literal constructor'); + + // TODO: add tests to see if importing and exporting of Item#data works }); \ No newline at end of file