ariatemplates / usermanual

This repo holds the articles for ariatemplates.com/usermanual (text content only)
http://ariatemplates.com/usermanual/latest/
5 stars 18 forks source link

IOFilters are not taken into account by Aria.load unless specific syntax is used #65

Open jakub-g opened 10 years ago

jakub-g commented 10 years ago

http://ariatemplates.com/usermanual/latest/filters

There are two issues regarding this article.

First, there's no sample about how to redirect to a static file (yeah it's in the api docs though this is pretty useful IMO so should be in the usermanual too).

This is mostly useful for mocking server interactions, i.e. return static JSON instead of contacting the backend. (i.e. usages of asyncRequest, submitJsonRequest etc.)

However, another thing I though it could be useful for is in a test, to redirect certain classpath to another classpath temporarily (well it could be done with URL maps too, but someone might come up with the same idea as me):

aria.core.IOFiltersMgr.addFilter({
    classpath : "test.aria.core.test.IOFilterSample",
    initArgs : {
        onRequest : function (req) {
            if (aria.utils.String.endsWith(req.url, "FooClass.js")) {
                this.redirectToFile(req, "myapp.classes.FooClassMock.js");
            }
        }
    }
});

or

aria.core.IOFiltersMgr.addFilter('myapp.classes.MyIOFilter'); // it has `onRequest` as above

However none of this works, because filters passed like that have to be loaded (via Aria.load) before proceeding further. The issue is that addFilter, when passed a string or an object with classpath, doesn't load the filters right away, they're loaded just before a request. When there's a request via Aria.load to load some class, the missing filters are not instantiated due to [1].

The bottom line is, the only way to have Aria.load take a filter into account, is to create an instance of it manually, and only then add that filter:

var myFilter = new myapp.classes.MyIOFilter();
aria.core.IOFiltersMgr.addFilter(myFilter);

Since there's an instance passed, this filter is applied right away (loading is not deferred until before-the-request).

This should be documented in the usermanual as an edge case.

[1] https://github.com/ariatemplates/ariatemplates/blob/v1.4.16/src/aria/core/IOFiltersMgr.js#L314