Re: libseed-list Hooking into the imports system



My concern here is that this would make code very difficult to understand.

Is there a way to implement this where it is an explicit behavior rather than an implicit, 

Hence, when it is being used, you know from the syntax that it may be trying to do something magical, rather than overloading the 'reasonably' predictable behavior of imports.

or could this not be done client side?
eg.

imports.smartloader.load('xxx');

---------
smartloader.js
function load(what) {
   var fname = workout_what(what);
   return imports[fname]
}

For the toString stuff, I guess that's needed only for this patch.


BTW i'll be offline tommorow till next week, unless I can get net access...

Regards
Alan


 --- On 30/Jun/2010, Jonatan Liljedahl wrote: 
> Here is a small patch that adds a feature that I'd be very happy to see.
> It allows one to hook into the imports system by defining a handler for
> when a module was not found. Example:
> 
>    imports.__notFoundHandler__ = function(name, dir) {
>      print("making "+name);
>      return {foo:123}
>    }
>    f = imports.foobar;
>    print(f.foo);
> 
> running above file prints:
> 
>    making foobar
>    123
> 
> This can be used to integrate all sorts of cool stuff with the imports
> object. For example, the handler can iterate over imports.searchPath to
> find n+".html" and return an object representation of the DOM tree, or
> parsing a custom language (hmmmm ;)), or perhaps even connect to an
> online database of modules.. x = imports.seedgems.foo; -> updates foo
> from an online repository, etc...
> 
> Currently it only passes the first path from seed_importer_search_dirs() 
> as the second arg, at least this works for the case when the search is 
> done inside a single directory, but I guess it would be better to just 
> send the whole path GSList by converting it to a JS array. One could 
> still work around it on the script side by merging the dir arg with 
> imports.searchPath if dir != searchPath[0].
> 
> BTW, this patch also adds the toString and toValue hack to 
> seed_importer_dir_get_property() so it doesn't call the handler saying 
> that "toString" module wasn't found, etc..
> 
> /Jonatan



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]