diff --git a/src/jquery.sketchable.js b/src/jquery.sketchable.js index 53f856d..06859e2 100644 --- a/src/jquery.sketchable.js +++ b/src/jquery.sketchable.js @@ -94,6 +94,24 @@ return $(this).data(namespace).options; } }, + /** + * Retrieve data associated to an existing Sketchable instance. + * @param {string} [property] - Top-level data property, e.g. "instance", "sketch", "options". + * @return {*} + * @memberof Sketchable + * @example + * // Read all the data associated to this instance. + * var data = $('canvas').sketchable('data'); + * // Quick access to the Sketchable instance. + * var inst = $('canvas').sketchable('data', 'instance'); + */ + data: function(property) { + var data = $(this).data(namespace); + + if (property) { + return data[property]; + } else { + return data; } }, /** diff --git a/src/sketchable.js b/src/sketchable.js index e19b002..68000e9 100644 --- a/src/sketchable.js +++ b/src/sketchable.js @@ -122,6 +122,24 @@ return data.options; } }, + /** + * Retrieve data associated to an existing Sketchable instance. + * @param {string} [property] - Top-level data property, e.g. "instance", "sketch", "options". + * @return {*} + * @memberof Sketchable + * @example + * var sketcher = new Sketchable('#my-canvas', { interactive: false }); + * // Read all the data associated to this instance. + * var data = sketcher.data(); + * // Quick access to Sketchable instance. + * var inst = sketcher.data('instance'); + */ + data: function(property) { + var elem = this.elem, data = dataBind(elem)[namespace]; + + if (property) { + return data[property]; + } else { return data; } },