Move 'data-paper-scope' attribute handling out of PaperScope into browser related code in PaperScript.

Closes #508
This commit is contained in:
Jürg Lehni 2014-08-16 18:24:45 +02:00
parent 288b8553e7
commit 5aa4a8300e
2 changed files with 12 additions and 12 deletions

View file

@ -189,9 +189,6 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
// Make sure this is the active scope, so the created project and view // Make sure this is the active scope, so the created project and view
// are automatically associated with it. // are automatically associated with it.
paper = this; paper = this;
// Link the element to this scope, so we can reuse the scope when
// compiling multiple scripts for the same element.
element.setAttribute('data-paper-scope', this._id);
// Create an empty project for the scope. // Create an empty project for the scope.
this.project = new Project(element); this.project = new Project(element);
// This is needed in PaperScript.load(). // This is needed in PaperScript.load().
@ -237,15 +234,11 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
_id: 0, _id: 0,
/** /**
* Retrieves a PaperScope object with the given id or associated * Retrieves a PaperScope object with the given scope id.
* with the passed canvas element.
* *
* @param id * @param id
*/ */
get: function(id) { get: function(id) {
// If an element is passed, get the id from it.
if (id && id.getAttribute)
id = id.getAttribute('data-paper-scope');
return this._scopes[id] || null; return this._scopes[id] || null;
}, },

View file

@ -449,14 +449,21 @@ Base.exports.PaperScript = (function() {
// retrieved through PaperScope.get(). // retrieved through PaperScope.get().
// If a canvas id is provided, pass it on to the PaperScope // If a canvas id is provided, pass it on to the PaperScope
// so a project is created for it now. // so a project is created for it now.
var canvas = PaperScope.getAttribute(script, 'canvas'), var canvasId = PaperScope.getAttribute(script, 'canvas'),
src = script.src; canvas = document.getElementById(canvasId),
canvas = document.getElementById(canvas) || canvas; src = script.src,
scopeKey = 'data-paper-scope';
if (!canvas)
throw new Error('Unable to find canvas with id "'
+ canvasId + '"');
// See if there already is a scope for this canvas and reuse // See if there already is a scope for this canvas and reuse
// it, to support multiple scripts per canvas. Otherwise // it, to support multiple scripts per canvas. Otherwise
// create a new one. // create a new one.
var scope = PaperScope.get(canvas) var scope = PaperScope.get(canvas.getAttribute(scopeKey))
|| new PaperScope().setup(canvas); || new PaperScope().setup(canvas);
// Link the element to this scope, so we can reuse the scope
// when compiling multiple scripts for the same element.
canvas.setAttribute(scopeKey, scope._id);
if (src) { if (src) {
// If we're loading from a source, request that first and // If we're loading from a source, request that first and
// then run later. // then run later.