Skip to content

Commit

Permalink
Fix race condition when rendering template on client-side with new sc…
Browse files Browse the repository at this point in the history
…ripts
  • Loading branch information
skerit committed Feb 11, 2016
1 parent e21e68b commit 6a6ed36
Showing 1 changed file with 64 additions and 45 deletions.
109 changes: 64 additions & 45 deletions lib/class/scene.js
Expand Up @@ -421,45 +421,56 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {

// Set the template this body comes from
if (!document.body.getAttribute('data-origin')) {
(function() {

template = renderData.lastTemplate;
var type,
data;

blockName = template.name + '__main__';
template = renderData.lastTemplate;

// Set the template origin
document.body.setAttribute('data-template', template.name);
blockName = template.name + '__main__';

// Set the name
document.body.setAttribute('data-name', blockName);
// Set the template origin
document.body.setAttribute('data-template', template.name);

// Set the active theme
document.body.setAttribute('data-theme', template.activeTheme);
// Set the name
document.body.setAttribute('data-name', blockName);

type = {
type : 'create',
category : 'block',
name : blockName,
template : template.name,
default : false,
root : true
};
// Set the active theme
document.body.setAttribute('data-theme', template.activeTheme);

data = {
name : type.name,
template : type.template,
default : false
}
type = {
type : 'create',
category : 'block',
name : blockName,
template : template.name,
default : false,
root : true
};

that.hawkejs.require(scripts, function() {
that.emit(type, document.body, renderData.variables, data);
});
data = {
name : type.name,
template : type.template,
default : false
}

that.hawkejs.require(scripts, function requiredScripts() {
that.emit(type, document.body, renderData.variables, data);
});
}());
}

// Emit an event for every new x-hawkejs block added to the DOM
while (this.newBlocks.length) {
Function.while(function test() {
return that.newBlocks.length > 0;
}, function doTask(next) {

var type,
data,
el;

// Get the first element
el = this.newBlocks[0];
el = that.newBlocks[0];

type = {
type : 'create',
Expand All @@ -484,10 +495,12 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {
el.className = el.className.replace(/\bjs-he-newblock(?!-)\b/g, '');

// Emit the created event
that.hawkejs.require(scripts, function() {
that.hawkejs.require(scripts, function requiredScripts() {
that.emit(type, el, renderData.variables, data);
});
}

next();
});

// Emit an event for every x-hawkejs block that gets new content
Blast.Bound.Object.each(renderData.blocks, function eachBlock(block, name) {
Expand Down Expand Up @@ -531,29 +544,31 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {
};

// Emit the set event
that.hawkejs.require(scripts, function() {
that.hawkejs.require(scripts, function requiredScripts() {
that.emit(type, el, variables, block);
});
});

if (renderData.dialogOpen) {
el = document.getElementById(renderData.dialogOpen);
(function() {
var el = document.getElementById(renderData.dialogOpen);

if (el) {
type = {
type : 'set',
category : 'dialog',
name : null,
target : null,
template : el.getAttribute('data-template'),
theme : el.getAttribute('data-theme') || 'default',
default : false
};
if (el) {
type = {
type : 'set',
category : 'dialog',
name : null,
target : null,
template : el.getAttribute('data-template'),
theme : el.getAttribute('data-theme') || 'default',
default : false
};

that.hawkejs.require(scripts, function() {
that.emit(type, el, renderData.variables);
});
}
that.hawkejs.require(scripts, function requiredScripts() {
that.emit(type, el, renderData.variables);
});
}
}());
}

that.emit({type: 'rendered', template: renderData.mainTemplate + ''}, renderData.variables, renderData);
Expand Down Expand Up @@ -2073,6 +2088,10 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {
var parent,
child;

if (!container) {
return;
}

if (typeof container == 'string') {
container = document.querySelector(container);
}
Expand Down Expand Up @@ -2164,7 +2183,7 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {
rect = this.getRelativeBoundingClientRect(container, toElement);

// If the top is already 0 nothing needs to happen
if (rect.top == 0) {
if (!rect || rect.top == 0) {
return;
}

Expand Down

0 comments on commit 6a6ed36

Please sign in to comment.