1

How do I take a set of dependencies and model them as a serial and parallel actions?

For example:

U1 depends upon U2

could be a serial representation of

S: [U1, U2]

U1 and U2 are not dependent would be a parallel representation.

P: [U1, U2]

S means serial, and P means parallel. In this example U refers to a URL that points to a resource.

In parallel resources the ordering does not matter. In serial resources the ordering does matter.

I'm trying to build a resource loader.

It only needs to work with the local server. Also, it only uses Ajax get requests to load resources.

But I want to a language/syntax to model the complexity of dependent resources.

Clarification and Examples

This is for learning/later implementation. No outside libraries unless you can relate the syntax they use for loading to the question.

Example 1

Jqueryui depends upon jquery. This would be represented as...

S: [U-jquery, U-jqueryui]

Example 2

jquery.js does not depend upon underscore.js. This would be represented as...

P: [U-jquery, U-underscore]

Example 3

backbone.js depends upon both underscore.js and jquery.js

P:[U-underscore, U-jquery] // group1
S:[some_notation to represent group1, U-backbone.js]
4

2 回答 2

1

No outside libraries unless you can relate the syntax they use for loading to the question.

You might want to have a look at require.js and how it specifies its dependencies. MediaWiki's ResourceLoader is also quite interesting, and though the JS dependency map is dynamically generated by the PHP script its structure might be relevant.

Jqueryui depends upon jquery. This would be represented as...

S: [U-jquery, U-jqueryui]

Possible, why not. However I'm not sure if you want to invent your own DSL for dependency management; if you rather want to stick with standard JSON / object literals I'd propose to use keys of an object for the modules whose dependencies you declare, and then lists as values:

deps = {
    "U-jqueryui": /*depends on*/ ["U-jquery" /*and nothing else*/]
};

jquery.js does not depend upon underscore.js. This would be represented as...

P: [U-jquery, U-underscore]

I don't understand. Why would you need to explicitly declare that? Everything that is unrelated (does not [explicitly] depend on each other somehow) should default to unordered loading/execution. Otherwise you would have an exponential amount of P-declarations for independent modules; and what should happen if you miss one?

backbone.js depends upon both underscore.js and jquery.js

P:[U-underscore, U-jquery] // group1
S:[some_notation to represent group1, U-backbone.js]

I would just use the same form as in example #1:

deps = {
    "U-backbone.js": /*depends on*/ ["U-underscore", /*and*/ "U-jquery"]
};

If you really need groups for something, you can create them like a module without a body, that just loads all its dependencies and provides them. In your loader code then make some exceptions for them (based on the name not starting with U- maybe?) so they don't fire actual ajax requests. Your final dependency object will then look like

deps = {
    "U-jqueryui": ["U-jquery"],
    "group1": ["U-underscore", "U-jquery"],
    "U-backbone.js": ["group1"]
};
于 2013-04-03T12:53:45.993 回答
-1

Check Modernizr.load:

There’s a good chance that you’re not terribly unhappy with your script downloading speeds now, but you’ll be happy to know that Modernizr.load does not slow anything down, and can sometimes offer a small boost in performance by loading scripts asynchronously and in parallel. There’s a lot of variables to weigh in this area, so we suggest that you try a few things to make sure you’re getting maximum performance for your specific situation.

http://modernizr.com/docs/

于 2013-03-30T23:09:01.833 回答