Skip to content

.path can now be a Function and also changed regexp to include "//" as full path (sometimes we don't want to specify the protocol!) #94

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

Open
wants to merge 3 commits 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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,32 @@ $script.ready('my-awesome-plugin', function() {

### $script.path()

Optionally to make working with large projects easier, there is a path variable you can set to set as a base.
Optionally to make working with large projects easier, there's a path variable that can be either a String or a Function (that will recieve the given path as first
argument):

``` js
// use a String as base path
$script.path('/js/modules/')
$script(['dom', 'event'], function () {
// /js/modules/dom.js and /js/modules/event.js will be loaded
// use dom & event
});

//Or use a function as base path
var fnc = function(path) {
//Please note that when using as a function, .js wont be attached automatically
return path === 'dom' ? ('//external.cdn.com/some/path/' + path + '.js') : ('/js/modules/' + path + '.js');
};

$script.path(fnc)
$script(['dom', 'event'], function () {
// external.cdn.com/some/path/dom.js and /js/modules/event.js will be loaded
// use dom & event
});
```

Please notice that the string ".js" is not appended automatically if a Function is used as path.

Note that this will include all scripts from here on out with the base path. If you wish to circumvent this for any single script, you can simply call <code>$script.get()</code>

``` js
Expand Down
11 changes: 9 additions & 2 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
return 1
})
}
function isFunction(object) {
return typeof(object) === 'function';
}

function $script(paths, idOrDone, optDone) {
paths = paths[push] ? paths : [paths]
Expand All @@ -50,8 +53,12 @@
each(paths, function loading(path, force) {
if (path === null) return callback()

if (!force && !/^https?:\/\//.test(path) && scriptpath) {
path = (path.indexOf('.js') === -1) ? scriptpath + path + '.js' : scriptpath + path;
if (!force && !/^(https?:)?\/\//.test(path) && scriptpath) {
if (isFunction(scripts)) {
path = scriptpath(path);
} else {
path = (path.indexOf('.js') === -1) ? scriptpath + path + '.js' : scriptpath + path;
}
}

if (scripts[path]) {
Expand Down