Implement Item#data. Closes #188.

This commit is contained in:
Jonathan Puckey 2013-03-10 14:59:53 +01:00
parent 78200e715b
commit 25d34be59b
2 changed files with 75 additions and 1 deletions

View file

@ -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}

View file

@ -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
});