Newbie, requiring JS.Class

61 views
Skip to first unread message

Yak

unread,
Oct 19, 2012, 8:38:12 PM10/19/12
to jsclas...@googlegroups.com
I am quite confused as how to best use this library. It appears that we have to require JS.Class in order to use anything. I couldn't find any reference to this in the documentation. This is what I have:

JS.Packages(function() { with(this) {
    file("https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js")
        .provides('jQuery')
        .requires("JS.Class");
   
    file("/red.js")
        .provides("Red")
        .requires("JS.Class");
       
    file("/blue.js")
        .provides("Blue")
        .requires("JS.Class");

    file('/app.js')
        .provides('Application')
        .requires('jQuery')
        .uses("Red")
        .uses("Blue")
}});

JS.require("Application", function() {
    var app = new Application();
    console.log("Application loaded");
});

Is this the proper way to do things? Will I have to require('JS.Class') on each of my files? If I put the require with the Application provider, I often get JS.Class not loaded errors.

James Coglan

unread,
Oct 23, 2012, 6:44:13 PM10/23/12
to jsclas...@googlegroups.com
Each file only needs to declare the things it *directly* depends on -- JS.Packages figures out the transitive dependencies and loads them for you. So for example, jQuery does not depend on JS.Class so you can remove that requires() line.

If every file in your system depends on the same thing, you may want to use namespaces and pattern-matching to help things along. See this post: http://blog.jcoglan.com/2012/01/23/organizing-a-project-with-js-packages/

Alternatively, make a helper to eliminate some boilerplate:

function appFile(path) {
  return file(path).requires('JS.Class');
};

appFile('/red.js').provides('Red');
appFile('/blue.js').provides('Blue');
Reply all
Reply to author
Forward
0 new messages