Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An extended and more robust injectScript #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# See https://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
Expand All @@ -13,5 +13,5 @@
/connect.lock
/coverage/*
/libpeerconnection.log
npm-debug.log
npm-debug.log*
testem.log
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"strict": false,
"white": false,
"eqnull": true,
"esnext": true,
"esversion": 6,
"unused": true
}
30 changes: 25 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
---
language: node_js
node_js:
- "4"

sudo: false

cache:
directories:
- node_modules
- $HOME/.npm
- $HOME/.cache # includes bowers cache

env:
# we recommend testing LTS's and latest stable release (bonus points to beta/canary)
- EMBER_TRY_SCENARIO=ember-lts-2.4
- EMBER_TRY_SCENARIO=ember-lts-2.8
- EMBER_TRY_SCENARIO=ember-release
- EMBER_TRY_SCENARIO=ember-beta
- EMBER_TRY_SCENARIO=ember-canary
- EMBER_TRY_SCENARIO=ember-default

matrix:
fast_finish: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
- "npm config set spin false"
- "npm install -g npm@^2"
- npm config set spin false
- npm install -g bower phantomjs-prebuilt
- bower --version
- phantomjs --version

install:
- npm install -g bower
- npm install
- bower install

script:
- npm test
# Usually, it's ok to finish the test scenario without reverting
# to the addon's original dependency state, skipping "cleanup".
- node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO test --skip-cleanup
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015
Copyright (c) 2017

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ npm install --save-dev ember-inject-script

For example, lets configure Typekit.

`loadScript` returns a promise, so we can simply load in the Typekit JS and, when it's ready, call `Typekit.load()`:
`loadScript` returns a promise, so we can simply load in the Typekit JS and, when it's ready, call `Typekit.load()`. If an error occurs fetching the script, we can catch the error and handle it:

```javascript
/* global Typekit */
Expand All @@ -24,11 +24,11 @@ import config from 'your-app/config/environment';

export default {
name: 'typekit',
initialize: function() {
var url = "//use.typekit.net/"+config.typekitID+".js";
injectScript(url).then(function() {
Typekit.load();
});
initialize() {
let url = "//use.typekit.net/"+config.typekitID+".js";
injectScript(url)
.then(() => Typekit.load())
.catch((error) => this.handleError(error));
}
};
```
Expand Down
32 changes: 23 additions & 9 deletions addon/utils/inject-script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import Ember from 'ember';

export default function injectScript(src) {
return new Ember.RSVP.Promise(function(resolve) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = src;
script.onload = function() { resolve(); };
document.getElementsByTagName('head')[0].appendChild(script);
let removeListenersAndElement = function(element) {
Ember.$(element).off('load');
Ember.$(element).off('error');
element.parentNode.removeChild(element);
};

export default function(url) {
return new Ember.RSVP.Promise((resolve, reject) => {
let script = document.createElement('script');
document.head.appendChild(script);
Ember.$(script).on('load', () => {
removeListenersAndElement(script);
resolve();
});
Ember.$(script).on('error', (error) => {
removeListenersAndElement(script);
reject(error);
});
script.asnyc = true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a typo here

script.type = 'text/javascript';
script.classList.add('test__promise-script');
script.src = url;
});
}
}
15 changes: 2 additions & 13 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
{
"name": "ember-inject-script",
"dependencies": {
"handlebars": "~1.3.0",
"jquery": "^1.11.1",
"ember": "1.8.1",
"ember-data": "1.0.0-beta.12",
"ember-resolver": "~0.1.11",
"loader.js": "stefanpenner/loader.js#1.0.1",
"ember-cli-shims": "stefanpenner/ember-cli-shims#0.0.3",
"ember-cli-test-loader": "rwjblue/ember-cli-test-loader#0.0.4",
"ember-load-initializers": "stefanpenner/ember-load-initializers#0.0.2",
"ember-qunit": "0.1.8",
"ember-qunit-notifications": "0.0.4",
"qunit": "~1.15.0"
"ember": "2.11.0"
}
}
}
91 changes: 91 additions & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*jshint node:true*/
module.exports = {
scenarios: [
{
name: 'ember-lts-2.4',
bower: {
dependencies: {
'ember': 'components/ember#lts-2-4'
},
resolutions: {
'ember': 'lts-2-4'
}
},
npm: {
devDependencies: {
'ember-source': null
}
}
},
{
name: 'ember-lts-2.8',
bower: {
dependencies: {
'ember': 'components/ember#lts-2-8'
},
resolutions: {
'ember': 'lts-2-8'
}
},
npm: {
devDependencies: {
'ember-source': null
}
}
},
{
name: 'ember-release',
bower: {
dependencies: {
'ember': 'components/ember#release'
},
resolutions: {
'ember': 'release'
}
},
npm: {
devDependencies: {
'ember-source': null
}
}
},
{
name: 'ember-beta',
bower: {
dependencies: {
'ember': 'components/ember#beta'
},
resolutions: {
'ember': 'beta'
}
},
npm: {
devDependencies: {
'ember-source': null
}
}
},
{
name: 'ember-canary',
bower: {
dependencies: {
'ember': 'components/ember#canary'
},
resolutions: {
'ember': 'canary'
}
},
npm: {
devDependencies: {
'ember-source': null
}
}
},
{
name: 'ember-default',
npm: {
devDependencies: {}
}
}
]
};
1 change: 1 addition & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*jshint node:true*/
'use strict';

module.exports = function(/* environment, appConfig */) {
Expand Down
18 changes: 18 additions & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*jshint node:true*/
/* global require, module */
var EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function(defaults) {
var app = new EmberAddon(defaults, {
// Add options here
});

/*
This build file specifies the options for the dummy test app of this
addon, located in `/tests/dummy`
This build file does *not* influence how the addon or the app using it
behave. You most likely want to be modifying `./index.js` or app's build file
*/

return app.toTree();
};
71 changes: 43 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
{
"name": "ember-inject-script",
"description": "inject 3rd party scripts into your ember-cli apps",
"version": "0.0.2",
"description": "Inject 3rd party scripts into your ember-cli apps",
"version": "0.0.3",
"keywords": [
"ember-addon",
"inject script",
"promise script",
"promise getScript"
],
"license": "MIT",
"author": {
"name": "Richard Livsey",
"email": "[email protected]"
},
"directories": {
"doc": "doc",
"test": "tests"
},
"repository": "https://github.com/minutebase/ember-inject-script",
"scripts": {
"start": "ember server",
"build": "ember build",
"start": "ember server",
"test": "ember test"
},
"repository": "https://github.com/minutebase/ember-inject-script",
"engines": {
"node": ">= 0.10.0"
},
"author": {
"name": "Richard Livsey",
"email": "[email protected]"
"dependencies": {
"ember-cli-babel": "^5.1.7"
},
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.0",
"broccoli-ember-hbs-template-compiler": "^1.6.1",
"ember-cli": "0.1.5",
"ember-cli-content-security-policy": "0.3.0",
"ember-cli-dependency-checker": "0.0.7",
"ember-cli-esnext": "0.1.1",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-qunit": "0.1.2",
"ember-data": "1.0.0-beta.12",
"ember-export-application-global": "^1.0.0",
"express": "^4.8.5",
"glob": "^4.0.5"
"broccoli-asset-rev": "^2.4.5",
"ember-ajax": "^2.4.1",
"ember-cli": "2.11.0",
"ember-cli-app-version": "^2.0.0",
"ember-cli-dependency-checker": "^1.3.0",
"ember-cli-htmlbars": "^1.1.1",
"ember-cli-htmlbars-inline-precompile": "^0.3.3",
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-jshint": "^2.0.1",
"ember-cli-qunit": "^3.0.1",
"ember-cli-release": "^0.2.9",
"ember-cli-shims": "^1.0.2",
"ember-cli-sri": "^2.1.0",
"ember-cli-test-loader": "^1.1.0",
"ember-cli-uglify": "^1.2.0",
"ember-data": "^2.11.0",
"ember-disable-prototype-extensions": "^1.1.0",
"ember-export-application-global": "^1.0.5",
"ember-load-initializers": "^0.6.0",
"ember-resolver": "^2.0.3",
"ember-source": "^2.11.0",
"ember-welcome-page": "^2.0.2",
"loader.js": "^4.0.10"
},
"engines": {
"node": ">= 0.12.0"
},
"keywords": [
"ember-addon",
"inject script"
],
"ember-addon": {
"configPath": "tests/dummy/config"
}
Expand Down
13 changes: 13 additions & 0 deletions testem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*jshint node:true*/
module.exports = {
"framework": "qunit",
"test_page": "tests/index.html?hidepassed",
"disable_watching": true,
"launch_in_ci": [
"PhantomJS"
],
"launch_in_dev": [
"PhantomJS",
"Chrome"
]
};
Loading