Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Using Titanium.include "dynamic"

Hi,

I've written a "Dispatcher" for my titanium mobile app which should dynamically include a controller. For example:

Titanium.include('controller/' + controller + 'Controller.js');

This does work only the first time. When I dispatch another "Request" using the dispatcher it crashes. The displayed error is always:

[83,0] file:///android_asset/Resources/library/mvc/Dispatcher.js
Message:
Wrapped java.lang.NullPointerException (file:///android_asset/Resources/library/mvc/Dispatcher.js#83)

Even an hard coded include of all Controllers results in the same behavior:

Titanium.include('controller/IndexController.js');
Titanium.include('controller/TestController.js');
Titanium.include('controller/OverviewController.js');

I even tried the following:

var handle = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'controller/' + controller + 'Controller.js');
eval(handle.read().text);

The eval works but the the controller contains a function and further javascript code like that:

function TestController() {
...
}
TestController.prototype = new ActionController();

so the function is not available afterwards. The dispatcher tries to instantiate the TestController and then an error occurrs that TestController is undefined.

I also tried:

var handle = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'controller/' + controller + 'Controller.js');
Titanium.include(fh.nativePath);

and

Titanium.include(Titanium.Filesystem.resourcesDirectory + 'controller/' + controller + 'Controller.js');

I'm using the dispatcher always to open a new window. The dispatcher does automatically include needed Controller + Views. The first window occurrs without problems. When I open the second window I always get an error.

I'm using Titanium Mobile 1.3.0

Can anyone help me? Thx

Greetz

Edit:
It works when I use the hard coded include at the beginning of my file Dispatcher.js:

Titanium.include('controller/IndexController.js');
Titanium.include('controller/TestController.js');
Titanium.include('controller/OverviewController.js');

function Dispatcher () {
...

But not when I use it within my function "dispatch" it does not work:

function Dispatcher () {
...
  this.dispatch = function () {
    Titanium.include('controller/IndexController.js');
    Titanium.include('controller/TestController.js');
    Titanium.include('controller/OverviewController.js');
  }
...
}

var dispatcher = new Dispatcher();
dispatcher.dispatch();
— asked June 12th 2010 by Jicks Steen
2 Comments
  • Hi Jicks, did you solve this dynamic include problem?

    — commented July 16th 2010 by Lukasz Radziwonowicz
  • It should be solved in 1.4.0, see this ticket.

    — commented July 16th 2010 by Lukasz Radziwonowicz

0 Answers

The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.