mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Implement Item#data. Closes #188.
This commit is contained in:
parent
78200e715b
commit
25d34be59b
2 changed files with 75 additions and 1 deletions
|
@ -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}
|
||||
|
|
|
@ -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
|
||||
});
|
Loading…
Reference in a new issue