Friday, 15 January 2010

javascript - how do I use curl.js to load an object? -



javascript - how do I use curl.js to load an object? -

curl 0.7.3

i got amd/commonjs adapter code here: supporting both commonjs , amd

(function (name, definition) { if (typeof module != 'undefined') { module.exports = definition(); } else if (typeof define == 'function' && typeof define.amd == 'object') { define(name, [], definition); } else { this[name] = definition(); } }('modxyz', { sayhi:function (name) { console.log('hi ' + name + '!'); } } ));

i'd utilize code curl create code amd/commonjs compatible. expecting able this:

greeter = curl(['modxyz']); greeter.sayhi("gracie");

but object curl returns isn't object i'm expecting. closest can this:

curl(['modxyz'], function(mod) { window.greeter = mod; }); greeter.sayhi("gracie");

which seems defeat purpose of amd. curl capable of doing this? have utilize require.js happen?

because browser remote resources, has either block main thread while fetches resources or has fetch them asynchronously. since should never block main thread (effectively making browser unresponsive), have fetch resources async. (it's same amd loader, requirejs, dojo, etc.)

therefore, things next can't work:

var foo = require('lib/foo');

well, can't work in usual global-ish space may used in browsers. can, however, work in controlled environment such within amd module.

step 1: write code within of modules.

step 2: write bootstrap module launch app.

<script src="lib/curl/src/curl.js"><script> <script src="myapp/run.js"><script>

inside run.js:

// curl 0.7.x requires named module here ('myapp/run') define('myapp/run', ['curl'], function (curl) { curl.config( baseurl: '', packages: [ /* configure 3rd-party packages */ ], paths: { /* configure non-package 3rd-party libs here */ }, preloads: [ /* set modules *must* loaded first here (e.g. shims) */ ], main: 'myapp/main' }); });

"myapp/main" this:

define(function (require) { var view1 = require('myapp/view1/controller'); view1.render(); view1.placeat('body'); });

inside main module (or amd module), require acts more think you're expecting. note special amd signature. can't specify dependencies in define , expect require behave this. have specify dependencies in only 1 way. not work:

define(['myapp/view1'], function (view1) { var foo = require('foo'); });

hope helps.

-- john

javascript module requirejs amd curl.js

No comments:

Post a Comment