Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Dynamic Module Generation

Received: by 10.42.161.71 with SMTP id s7mr1477275icx.87.1310455219337;
        Tue, 12 Jul 2011 00:20:19 -0700 (PDT)
X-BeenThere: oni-apollo@googlegroups.com
Received: by 10.231.185.5 with SMTP id cm5ls1698800ibb.3.gmail; Tue, 12 Jul
 2011 00:20:18 -0700 (PDT)
Received: by 10.42.163.129 with SMTP id c1mr1191113icy.29.1310455218536;
        Tue, 12 Jul 2011 00:20:18 -0700 (PDT)
Received: by 10.42.163.129 with SMTP id c1mr1191112icy.29.1310455218519;
        Tue, 12 Jul 2011 00:20:18 -0700 (PDT)
Return-Path: <a...@onilabs.com>
Received: from mail-iy0-f171.google.com (mail-iy0-f171.google.com [209.85.210.171])
        by gmr-mx.google.com with ESMTPS id w7si5506987icv.6.2011.07.12.00.20.17
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 12 Jul 2011 00:20:17 -0700 (PDT)
Received-SPF: neutral (google.com: 209.85.210.171 is neither permitted nor denied by best guess record for domain of a...@onilabs.com) client-ip=209.85.210.171;
Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 209.85.210.171 is neither permitted nor denied by best guess record for domain of a...@onilabs.com) smtp.mail=a...@onilabs.com
Received: by iyi12 with SMTP id 12so5376487iyi.30
        for <oni-apollo@googlegroups.com>; Tue, 12 Jul 2011 00:20:17 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.42.83.194 with SMTP id i2mr6557998icl.305.1310455215852; Tue,
 12 Jul 2011 00:20:15 -0700 (PDT)
Received: by 10.42.226.67 with HTTP; Tue, 12 Jul 2011 00:20:15 -0700 (PDT)
X-Originating-IP: [83.84.224.61]
In-Reply-To: <93252053-3de2-466f-b828-a2785d9c9508@z14g2000yqh.googlegroups.com>
References: <00535dc7-d317-4eb6-8e28-4115aba331cf@x12g2000yql.googlegroups.com>
	<CAGso3h5bjHc1c=gqAii3c7e49ro+9uEQAKO4xhytKoR8Ob6PkQ@mail.gmail.com>
	<963d4724-58c0-44c9-8138-b1cf35b71e5a@r18g2000vbs.googlegroups.com>
	<93252053-3de2-466f-b828-a2785d9c9508@z14g2000yqh.googlegroups.com>
Date: Tue, 12 Jul 2011 09:20:15 +0200
Message-ID: <CAGso3h5+uw4d81vdvzGynmUmjGKZeSB16n4fYEzvG9stJUKz1A@mail.gmail.com>
Subject: Re: [oni-apollo] Re: Dynamic Module Generation
From: Alexander Fritze <a...@onilabs.com>
To: oni-apollo@googlegroups.com
Content-Type: multipart/alternative; boundary=90e6ba5bc255a28bba04a7da21cb

--90e6ba5bc255a28bba04a7da21cb
Content-Type: text/plain; charset=ISO-8859-1

Ah yes, the current trunk is a little bit more restrictive vs version 0.12
on what you can put into require.hubs (for see rationale below).

Basically you can only put *absolute* urls into require.hubs. So the easiest
way to get your setup to work is to do:

  window.require.hubs.push(["durandal:", "http://YOUR-SERVER/durandal/"]);

Alternatively, you can do this:

  var sys = require("durandal/system");

or this:

  require.alias.durandal = "/durandal";
  var sys = require("durandal:system");

Note also that if you have several modules in the /durandal folder (let's
say A.sjs and B.sjs), they can refer to each other without any prefix, but
using './' (this is how it works in nodejs, and for interoperability
reasons, apollo is adopting this):

main program:
  require('/durandal/A');

A.sjs:
  require('./B'); // preferred way. Works in browser-based and nodejs
apollo.
  // require('durandal:B'); will also work, if you've set up a global
absolute url for durandal: in require.hubs.
  // require('B') will also work, but only in the browser-based apollo.



** Rationale **

require.hubs is a *global* method for setting endpoints from where modules
are loaded. If we allowed relative urls here, it wouldn't be clear under all
circumstances what they are relative to. E.g. with the example of A.sjs and
B.sjs sitting under /durandal, if you require("durandal:B") from A.sjs, and
the hub is set up to be relative ('durandal:', 'durandal/'), then the module
system would look for B at /durandal/durandal/B.sjs.

require.alias on the other hand is a *local* method for rewriting urls. It
only applies to the file/module where you set it. So it is safe to provide a
relative URL here; the module system always knows what the correct base
should be.

Hope this helps,

Cheers,
Alex

On Mon, Jul 11, 2011 at 10:20 PM, EisenbergEffect
<rob.eisenb...@gmail.com>wrote:

> Well, I went to try it out and I think that some other scenarios may
> have been broken. For example, I have this code as part of my startup
> sequence:
>
> window.require.hubs.push(["durandal:", "/durandal/"]);
> var sys = require("durandal:system");
>
> In this case, the call to require "durandal:system" fails with the
> message "Cannot load module 'durandal:system'. (Underlying exception:
> Error: Don't know how to load module at /durandal/system.sjs (in
> apollo-sys-common.sjs:445)) (in apollo-sys-common.sjs:612)"
>
> I changed nothing in my source code except that I dropped in the new
> apollo version for the above github repo. Any ideas?
>
> On Jul 11, 9:01 am, EisenbergEffect <rob.eisenb...@gmail.com> wrote:
> > Thank you so much! This is going to be beautiful.
> >
> > On Jul 11, 8:43 am, Alexander Fritze <a...@onilabs.com> wrote:
> >
> >
> >
> >
> >
> >
> >
> > > On Fri, Jul 8, 2011 at 7:24 PM, EisenbergEffect <
> rob.eisenb...@gmail.com>wrote:
> >
> > > > I have a situation where I need to dynamically generate modules and
> > > > eval them. Is there an easy way to do this? Thanks!
> >
> > > There is now. I just committed functionality to github trunk (
> https://github.com/onilabs/apollo, mirrored athttp://
> code.onilabs.com/apollo/unstable, play around with it in the sandbox
> > > here:http://code.onilabs.com/apollo/unstable/demo/sandbox.html) that
> > > allows you to do the following:
> >
> > > require('some-prefix:some-modulename', { src: "SOURCE STRING FOR YOUR
> > > MODULE"})
> >
> > > E.g.:
> >
> > > var test = require('local:test', { src: "exports.foo = function(x) {
> return
> > > 'Hello, you said: '+x;};" })
> >
> > > test.foo("bar"); // --> Hello, you said: bar
> >
> > > require("local:test").foo("baz"); // --> Hello, you said: baz
> >
> > > Caveats:
> >
> > > - You need to give the module a name *with prefix* (e.g. something like
> > > 'local:'). If you don't, then the module system might load something
> that
> > > you don't expect. E.g. in apollo running on nodejs, if you execute
> > > require('fs', { src: "export.foo=43" }), it will ignore your src
> parameter,
> > > and load nodejs's 'fs' module instead.
> >
> > > - Preferably the prefix you choose should be something that doesn't
> appear
> > > in your modules.hubs array (i.e. not 'http:', 'https:", "github:",
> "apollo:"
> > > + whatever else you might have added in there.). Otherwise unexpected
> things
> > > might happen (i.e. something else being loaded than you expect, like in
> the
> > > no-prefix case).
> >
> > > There's more things you can do; I'll document this more fully soon (it
> also
> > > forms the basis for CoffeeScript cross-compilation).
> >
> > > Cheers,
> > > Alex
>

--90e6ba5bc255a28bba04a7da21cb
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Ah yes, the current trunk is a little bit more restrictive vs version 0.12 =
on what you can put into require.hubs (for see rationale below).<div><br></=
div><div>Basically you can only put *absolute* urls into require.hubs. So t=
he easiest way to get your setup to work is to do:</div>
<div><br></div><div>=A0 window.require.hubs.push([&quot;durandal:&quot;, &q=
uot;<a href=3D"http://YOUR-SERVER/durandal/">http://YOUR-SERVER/durandal/</=
a>&quot;]);</div><div><br></div><div>Alternatively, you can do this:</div><=
div>
<br></div><div>=A0 var sys =3D require(&quot;durandal/system&quot;);</div><=
div><br></div><div>or this:</div><div><br></div><div>=A0 require.alias.dura=
ndal =3D &quot;/durandal&quot;;</div><div>=A0 var sys =3D require(&quot;dur=
andal:system&quot;);</div>
<div><br></div><div>Note also that if you have several modules in the /dura=
ndal folder (let&#39;s say A.sjs and B.sjs), they can refer to each other w=
ithout any prefix, but using &#39;./&#39; (this is how it works in nodejs, =
and for interoperability reasons, apollo is adopting this):</div>
<div><br></div><div>main program:</div><div>=A0 require(&#39;/durandal/A&#3=
9;);</div><div><br></div><div>A.sjs:</div><div>=A0 require(&#39;./B&#39;); =
// preferred way. Works in browser-based and nodejs apollo.</div><div>=A0 /=
/ require(&#39;durandal:B&#39;); will also work, if you&#39;ve set up a glo=
bal absolute url for durandal: in require.hubs.</div>
<div>=A0 // require(&#39;B&#39;) will also work, but only in the browser-ba=
sed apollo.</div><div><br></div><div><br></div><div><br></div><div>** Ratio=
nale **</div><div><br></div><div>require.hubs is a *global* method for sett=
ing endpoints from where modules are loaded. If we allowed relative urls he=
re, it wouldn&#39;t be clear under all circumstances what they are relative=
 to. E.g. with the example of A.sjs and B.sjs sitting under /durandal, if y=
ou require(&quot;durandal:B&quot;) from A.sjs, and the hub is set up to be =
relative (&#39;durandal:&#39;, &#39;durandal/&#39;), then the module system=
 would look for B at /durandal/durandal/B.sjs.</div>
<div><br></div><div>require.alias on the other hand is a *local* method for=
 rewriting urls. It only applies to the file/module where you set it. So it=
 is safe to provide a relative URL here; the module system always knows wha=
t the correct base should be.</div>
<div><br></div><div>Hope this helps,</div><div><br></div><div>Cheers,</div>=
<div>Alex</div><div><br></div><div><div class=3D"gmail_quote">On Mon, Jul 1=
1, 2011 at 10:20 PM, EisenbergEffect <span dir=3D"ltr">&lt;<a href=3D"mailt=
o:rob.eisenb...@gmail.com" target=3D"_blank">rob.eisenb...@gmail.com</a>&gt=
;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Well, I went to try it out and I think that some other scenarios may<br>
have been broken. For example, I have this code as part of my startup<br>
sequence:<br>
<br>
window.require.hubs.push([&quot;durandal:&quot;, &quot;/durandal/&quot;]);<=
br>
var sys =3D require(&quot;durandal:system&quot;);<br>
<br>
In this case, the call to require &quot;durandal:system&quot; fails with th=
e<br>
message &quot;Cannot load module &#39;durandal:system&#39;. (Underlying exc=
eption:<br>
Error: Don&#39;t know how to load module at /durandal/system.sjs (in<br>
apollo-sys-common.sjs:445)) (in apollo-sys-common.sjs:612)&quot;<br>
<br>
I changed nothing in my source code except that I dropped in the new<br>
apollo version for the above github repo. Any ideas?<br>
<div><div></div><div><br>
On Jul 11, 9:01=A0am, EisenbergEffect &lt;<a href=3D"mailto:rob.eisenb...@g=
mail.com" target=3D"_blank">rob.eisenb...@gmail.com</a>&gt; wrote:<br>
&gt; Thank you so much! This is going to be beautiful.<br>
&gt;<br>
&gt; On Jul 11, 8:43=A0am, Alexander Fritze &lt;<a href=3D"mailto:a...@onil=
abs.com" target=3D"_blank">a...@onilabs.com</a>&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; &gt; On Fri, Jul 8, 2011 at 7:24 PM, EisenbergEffect &lt;<a href=3D"ma=
ilto:rob.eisenb...@gmail.com" target=3D"_blank">rob.eisenb...@gmail.com</a>=
&gt;wrote:<br>
&gt;<br>
&gt; &gt; &gt; I have a situation where I need to dynamically generate modu=
les and<br>
&gt; &gt; &gt; eval them. Is there an easy way to do this? Thanks!<br>
&gt;<br>
&gt; &gt; There is now. I just committed functionality to github trunk (<a =
href=3D"https://github.com/onilabs/apollo" target=3D"_blank">https://github=
.com/onilabs/apollo</a>, mirrored athttp://<a href=3D"http://code.onilabs.c=
om/apollo/unstable" target=3D"_blank">code.onilabs.com/apollo/unstable</a>,=
 play around with it in the sandbox<br>


&gt; &gt; here:<a href=3D"http://code.onilabs.com/apollo/unstable/demo/sand=
box.html" target=3D"_blank">http://code.onilabs.com/apollo/unstable/demo/sa=
ndbox.html</a>) that<br>
&gt; &gt; allows you to do the following:<br>
&gt;<br>
&gt; &gt; require(&#39;some-prefix:some-modulename&#39;, { src: &quot;SOURC=
E STRING FOR YOUR<br>
&gt; &gt; MODULE&quot;})<br>
&gt;<br>
&gt; &gt; E.g.:<br>
&gt;<br>
&gt; &gt; var test =3D require(&#39;local:test&#39;, { src: &quot;exports.f=
oo =3D function(x) { return<br>
&gt; &gt; &#39;Hello, you said: &#39;+x;};&quot; })<br>
&gt;<br>
&gt; &gt; test.foo(&quot;bar&quot;); // --&gt; Hello, you said: bar<br>
&gt;<br>
&gt; &gt; require(&quot;local:test&quot;).foo(&quot;baz&quot;); // --&gt; H=
ello, you said: baz<br>
&gt;<br>
&gt; &gt; Caveats:<br>
&gt;<br>
&gt; &gt; - You need to give the module a name *with prefix* (e.g. somethin=
g like<br>
&gt; &gt; &#39;local:&#39;). If you don&#39;t, then the module system might=
 load something that<br>
&gt; &gt; you don&#39;t expect. E.g. in apollo running on nodejs, if you ex=
ecute<br>
&gt; &gt; require(&#39;fs&#39;, { src: &quot;export.foo=3D43&quot; }), it w=
ill ignore your src parameter,<br>
&gt; &gt; and load nodejs&#39;s &#39;fs&#39; module instead.<br>
&gt;<br>
&gt; &gt; - Preferably the prefix you choose should be something that doesn=
&#39;t appear<br>
&gt; &gt; in your modules.hubs array (i.e. not &#39;http:&#39;, &#39;https:=
&quot;, &quot;github:&quot;, &quot;apollo:&quot;<br>
&gt; &gt; + whatever else you might have added in there.). Otherwise unexpe=
cted things<br>
&gt; &gt; might happen (i.e. something else being loaded than you expect, l=
ike in the<br>
&gt; &gt; no-prefix case).<br>
&gt;<br>
&gt; &gt; There&#39;s more things you can do; I&#39;ll document this more f=
ully soon (it also<br>
&gt; &gt; forms the basis for CoffeeScript cross-compilation).<br>
&gt;<br>
&gt; &gt; Cheers,<br>
&gt; &gt; Alex<br>
</div></div></blockquote></div><br></div>

--90e6ba5bc255a28bba04a7da21cb--