Skip to content

Commit

Permalink
Init correctly components with javascript. Fixes GrapesJS#873
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Feb 27, 2018
1 parent e60a5bc commit feb9a8b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/canvas/view/CanvasView.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,22 +328,24 @@ module.exports = Backbone.View.extend({
this.getJsContainer().append(view.scriptContainer.get(0));
}

var model = view.model;
var id = model.getId();
const model = view.model;
const id = model.getId();
view.el.id = id;
view.scriptContainer.html('');
// In editor, I make use of setTimeout as during the append process of elements
// those will not be available immediatly, therefore 'item' variable
const script = document.createElement('script');
script.innerText = `
script.innerHTML = `
setTimeout(function() {
var item = document.getElementById('${id}');
if (!item) return;
(function(){
${model.getScriptString()};
}.bind(item))()
}, 1);`;
view.scriptContainer.get(0).appendChild(script);
// #873
// Adding setTimeout will make js components work on init of the editor
setTimeout(() => view.scriptContainer.get(0).appendChild(script), 0);
},

/**
Expand Down

0 comments on commit feb9a8b

Please sign in to comment.