Skip to content
This repository was archived by the owner on Oct 1, 2023. It is now read-only.

Commit 0597eb3

Browse files
committed
Initial version (0.1.0). Defines loadScript() and loadCSS() methods
1 parent c5ad85f commit 0597eb3

14 files changed

+421
-1
lines changed

.editorconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
# Change these settings to your own preference
10+
indent_style = tab
11+
indent_size = 4
12+
13+
# We recommend you to keep these unchanged
14+
end_of_line = lf
15+
charset = utf-8
16+
trim_trailing_whitespace = true
17+
insert_final_newline = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false
21+
22+
[{bower.json,package.json}]
23+
indent_style = space
24+
indent_size = 4

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.idea
2+
/bower_components
3+
/node_modules
4+
/coverage

.jshintrc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"node": true,
3+
"browser": true,
4+
"esnext": true,
5+
"bitwise": true,
6+
"camelcase": true,
7+
"curly": true,
8+
"eqeqeq": true,
9+
"immed": true,
10+
"indent": 2,
11+
"latedef": true,
12+
"newcap": true,
13+
"noarg": true,
14+
"quotmark": "single",
15+
"regexp": true,
16+
"undef": true,
17+
"unused": true,
18+
"strict": true,
19+
"trailing": true,
20+
"smarttabs": true,
21+
"maxdepth": 2,
22+
"maxcomplexity": 10,
23+
"globals": {
24+
"angular": false
25+
}
26+
}

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
bower_components
3+
node_modules
4+
coverage

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
before_script:
5+
- npm install -g [email protected] grunt-cli
6+
- bower install
7+
after_success:
8+
- cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Changelog
2+
3+
## 0.1.0 - 2014-03-04
4+
- Initial release

Gruntfile.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* License: MIT.
2+
* Copyright (C) 2014, Uri Shaked.
3+
*/
4+
5+
'use strict';
6+
7+
module.exports = function (grunt) {
8+
// load all grunt tasks
9+
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
10+
11+
grunt.initConfig({
12+
karma: {
13+
unit: {
14+
configFile: 'karma.conf.js',
15+
singleRun: true
16+
}
17+
},
18+
jshint: {
19+
options: {
20+
jshintrc: '.jshintrc'
21+
},
22+
all: [
23+
'Gruntfile.js',
24+
'angular-load.js',
25+
'tests.js'
26+
]
27+
},
28+
uglify: {
29+
dist: {
30+
files: {
31+
'angular-load.min.js': 'angular-load.js'
32+
}
33+
}
34+
}
35+
});
36+
37+
grunt.registerTask('test', [
38+
'jshint',
39+
'karma'
40+
]);
41+
42+
grunt.registerTask('build', [
43+
'jshint',
44+
'uglify'
45+
]);
46+
47+
grunt.registerTask('default', ['build']);
48+
};

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
11
angular-load
22
============
33

4-
Dynamically load scripts and css stylesheets in your Angular.JS app
4+
Dynamically load scripts and css stylesheets in your Angular.JS app.
5+
6+
Copyright (C) 2014, Uri Shaked <[email protected]>
7+
8+
[![Build Status](https://travis-ci.org/urish/angular-load.png?branch=master)](https://travis-ci.org/urish/angular-load)
9+
[![Coverage Status](https://coveralls.io/repos/urish/angular-load/badge.png)](https://coveralls.io/r/urish/angular-load)
10+
11+
Installation
12+
------------
13+
14+
You can choose your preferred method of installation:
15+
* Through bower: `bower install angular-load --save`
16+
* Through npm: `npm install angular-load --save`
17+
* Download from github: [angular-load.min.js](https://raw.github.com/urish/angular-load/master/angular-load.min.js)
18+
19+
Usage
20+
-----
21+
Include angular-load.js in your application.
22+
23+
```html
24+
<script src="bower_components/angular-load/angular-load.js"></script>
25+
```
26+
27+
Add the module `urish.load` as a dependency to your app module:
28+
29+
```js
30+
var myapp = angular.module('myapp', ['urish.load']);
31+
```
32+
33+
### angularLoad service directive
34+
The angularLoad service provides two methods: `loadScript()` and `loadCSS()`. Call these methods to load a script
35+
or a CSS stylesheet asynchronously into the current page. Both methods return a promise that will be resolved
36+
once the resource (script or stylesheet) has been loaded. In case of an error (e.g. HTTP 404) the promise will be
37+
rejected.
38+
39+
Usage example:
40+
41+
```js
42+
angularLoad.loadScript('https://mysite.com/someplugin.js').then(function() {
43+
// Script loaded succesfully.
44+
// We can now start using the functions from someplugin.js
45+
}).catch(function() {
46+
// There was some error loading the script. Meh
47+
});
48+
```
49+
50+
License
51+
----
52+
53+
Released under the terms of MIT License:
54+
55+
Permission is hereby granted, free of charge, to any person obtaining
56+
a copy of this software and associated documentation files (the
57+
'Software'), to deal in the Software without restriction, including
58+
without limitation the rights to use, copy, modify, merge, publish,
59+
distribute, sublicense, and/or sell copies of the Software, and to
60+
permit persons to whom the Software is furnished to do so, subject to
61+
the following conditions:
62+
63+
The above copyright notice and this permission notice shall be
64+
included in all copies or substantial portions of the Software.
65+
66+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
67+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
70+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
71+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
72+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

angular-load.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* angular-load.js / v0.1.0 / (c) 2014 Uri Shaked / MIT Licence */
2+
3+
(function () {
4+
'use strict';
5+
6+
angular.module('urish.load', [])
7+
.service('angularLoad', ['$document', '$q', '$timeout', function ($document, $q, $timeout) {
8+
9+
/**
10+
* Dynamically loads the given script
11+
* @param src The url of the script to load dynamically
12+
* @returns {*} Promise that will be resolved once the script has been loaded.
13+
*/
14+
this.loadScript = function (src) {
15+
var deferred = $q.defer();
16+
var script = $document[0].createElement('script');
17+
script.onload = function (e) {
18+
$timeout(function () {
19+
deferred.resolve(e);
20+
});
21+
};
22+
script.onerror = function (e) {
23+
$timeout(function () {
24+
deferred.reject(e);
25+
});
26+
};
27+
script.src = src;
28+
$document[0].body.appendChild(script);
29+
return deferred.promise;
30+
};
31+
32+
/**
33+
* Dynamically loads the given CSS file
34+
* @param href The url of the CSS to load dynamically
35+
* @returns {*} Promise that will be resolved once the CSS file has been loaded.
36+
*/
37+
this.loadCSS = function (href) {
38+
var deferred = $q.defer();
39+
var style = $document[0].createElement('link');
40+
style.rel = 'stylesheet';
41+
style.type = 'text/css';
42+
style.href = href;
43+
style.onload = function (e) {
44+
$timeout(function () {
45+
deferred.resolve(e);
46+
});
47+
};
48+
style.onerror = function (e) {
49+
$timeout(function () {
50+
deferred.reject(e);
51+
});
52+
};
53+
$document[0].head.appendChild(style);
54+
return deferred.promise;
55+
};
56+
}]);
57+
})();

angular-load.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "angular-load",
3+
"version": "0.1.0",
4+
"description": "Dynamically load scripts and css stylesheets in your Angular.JS app",
5+
"author": "Uri Shaked",
6+
"license": "MIT",
7+
"homepage": "http://github.com/urish/angular-load",
8+
"main": "./angular-load.js",
9+
"dependencies": {
10+
"angular": ">=1.0.0 <1.3.0"
11+
},
12+
"devDependencies": {
13+
"angular-mocks": "1.2.x"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git://github.com/urish/angular-load.git"
18+
}
19+
}

karma.conf.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* License: MIT.
2+
* Copyright (C) 2014, Uri Shaked.
3+
*/
4+
5+
'use strict';
6+
7+
module.exports = function (config) {
8+
config.set({
9+
basePath: '',
10+
frameworks: ['jasmine'],
11+
logLevel: config.LOG_INFO,
12+
browsers: ['PhantomJS'],
13+
autoWatch: true,
14+
reporters: ['dots', 'coverage'],
15+
files: [
16+
'bower_components/angular/angular.js',
17+
'bower_components/angular-mocks/angular-mocks.js',
18+
'angular-load.js',
19+
'tests.js'
20+
],
21+
preprocessors: {
22+
'angular-load.js': 'coverage'
23+
},
24+
coverageReporter: {
25+
type: 'lcov',
26+
dir: 'coverage/'
27+
}
28+
});
29+
};

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "angular-load",
3+
"version": "0.1.0",
4+
"repository": {
5+
"type": "git",
6+
"url": "http://github.com/urish/angular-load.git"
7+
},
8+
"dependencies": {},
9+
"devDependencies": {
10+
"matchdep": "~0.3.0",
11+
"grunt": "~0.4.1",
12+
"grunt-contrib-uglify": "~0.3.0",
13+
"grunt-contrib-jshint": "~0.8.0",
14+
"grunt-karma": "~0.7.2",
15+
"karma": "~0.11.10",
16+
"karma-coverage": "~0.2.0",
17+
"karma-jasmine": "~0.2.1",
18+
"karma-phantomjs-launcher": "~0.1.1",
19+
"coveralls": "~2.7.1"
20+
},
21+
"engines": {
22+
"node": ">=0.10.0"
23+
},
24+
"scripts": {
25+
"test": "grunt test"
26+
}
27+
}

0 commit comments

Comments
 (0)