A question about Loader baseURL

Sam Tobin-Hochstadt samth at ccs.neu.edu
Mon Aug 27 07:14:40 PDT 2012


On Mon, Aug 27, 2012 at 10:07 AM, Shijun He <hax.sfo at gmail.com> wrote:
> On Mon, Aug 27, 2012 at 8:10 PM, Sam Tobin-Hochstadt <samth at ccs.neu.edu> wrote:
>>> System.baseURL // "lib/a.js" ??
>>
>> No, the base url will be an absolute URL, like 'http://example.com/lib/a.js'.
>
> So in /lib/b.js , System.baseURL will return a diff result
> "http://example.com/lib/b.js" ?

This depends on how you're loading "b.js".

>
>>> var myLoader = new Loader(System, { baseURL: 'mypath/c.js' , fetch:myFetch })
>>>
>>> myLoader.baseURL // 'mypath/c.js' or 'lib/mypath/c.js' or any other value?
>>
>> 'mypath/c.js', which is what you provided as the 'baseURL'.
>>
>>> myLoader.load('b.js' ...) // load 'lib/mypath/b.js' or 'lib/b.js' ?
>>
>> Neither -- this invokes the `fetch` hook with relative url 'b.js', and
>> the base url 'mypath/c.js' -- it's up to the fetch hook to determine
>> what to do with it.  But 'lib' isn't in either of those arguments
>> (although `myFetch` might contain a reference to `System.baseURL`).
>
> So how do myLoader know what is the URL of the caller?! I mean:
>
> I want:
>
> /a/*.js
> myLoader.load('test.js')  // load /a/test.js
>
> /b/*.js
> myLoader.load('test.js')  // load /b/test.js
>
> But myFetch get the identical args: ('test.js', myLoader.baseURL)
>
> HOW can myFetch know the correct URL? (Holding System.baseURL seems
> useless in this case.)

`load` is a method, which can be called anywhere.  Having it depend on
a dynamically-scoped bit of information such as the url of the file
that the method is called in, would make using loaders much harder to
understand.

This is just like current browser behavior when you insert a `<script
src="test.js">` tag -- "test.js" is looked up relative to the URL of
the *page*, not relative to the URL of the js file that does the dom
manipulation.
-- 
sam th
samth at ccs.neu.edu


More information about the es-discuss mailing list