Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2658 from Tahi-project/chores/inline-ember-cli-la…
…zyloader APERTA-7947 chore/Inline ember-cli-lazyloader
- Loading branch information
Showing
6 changed files
with
4,063 additions
and
3,203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* global $ */ | ||
import Ember from 'ember'; | ||
|
||
var Promise = Ember.RSVP.Promise; | ||
|
||
var _loadedScripts = {}; | ||
|
||
// This version injects a script instead of using global.eval | ||
// which eases debugging (e.g., stacktraces make sense) | ||
var injectScript = function(src, cb) { | ||
Ember.Logger.info('Loading script %s', src); | ||
if (_loadedScripts[src]) { | ||
return cb(); | ||
} | ||
var headEl = document.head || document.getElementsByTagName('head')[0]; | ||
var scriptEl = window.document.createElement('script'); | ||
scriptEl.type = 'text\/javascript'; | ||
scriptEl.src = src; | ||
scriptEl.onload = function() { | ||
_loadedScripts[src] = true; | ||
cb(); | ||
}; | ||
scriptEl.onerror = function (error) { | ||
var err = new URIError('The script ' + error.target.src + ' is not accessible.'); | ||
console.error('Could not load', src); | ||
cb(err); | ||
}; | ||
headEl.appendChild(scriptEl); | ||
}; | ||
|
||
export default { | ||
loadScripts: function(urls) { | ||
// if assets are included in the bundle, then just initialize the platform | ||
return new Promise(function(resolve, reject) { | ||
var i = 0; | ||
function loadScript(err) { | ||
if (err) { | ||
reject(err); | ||
} else if (i >= urls.length) { | ||
resolve(); | ||
} else { | ||
injectScript(urls[i++], loadScript); | ||
} | ||
} | ||
// start the loading sequence | ||
loadScript(); | ||
}); | ||
}, | ||
loadCSS: function(url) { | ||
if (!_loadedScripts[url]) { | ||
$('<link/>', { | ||
rel: 'stylesheet', | ||
type: 'text/css', | ||
href: url | ||
}).appendTo('head'); | ||
_loadedScripts[url] = true; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.