Include js file using absolute path
Pretty basic question, but it's been giving me issues for a while - how do you include a javascript file using an absolute path? The following doesn't seem to work:
Ti.include("/folder/file.js");
If I use a relative path from the current file, it's fine, but that will get messy pretty quick. Is there some other way to reference the root directory?
14 Answers
-
Not as easy as I'd like it to be, but this works fine:
Ti.include(Titanium.Filesystem.resourcesDirectory+'file.js');
-
Here's a small, contrived example:
If you want to run it, put a Content.db database file in "Resources/database/" (doesn't matter what's in the database).
So basically, DataAccess.js just defines a db communication object which installs Content.db in the constructor. Inside app.js, I can create an instance of DataAccess and everything works fine, because the install path is relative to the Resources folder. However, if I try to create an instance of DataAccess inside someFile.js, which is created as a new window by app.js, the application will crash because the database install path is no longer correct. (Titanium is looking relative to the "Resources/include" folder)
Now it's not so bad to work around the problem in a small example like this with just a couple files. But I'm trying to build a fairly deep directory structure, where many files would be including a base DataAccess.js file.
-
can we create visualizer in titanium
-
I have this problem too. I have a single JS file that includes all of the other JS files I need. But that doesn't work in any other path. How?
-
This is still an issue for me. I have several files that all include a base Database.js file, which contains a Database.install(…) command. But the path to the database changes for the different files that include Database.js. I just need some way to start from the Resources folder, rather than always having to provide a relative path.
-
You can use
Ti.includeAbsolute
to include starting from theResources
directory.It is an undocumented API, but it works a lot better. (on every platform)
-
'Ti.include(path/file);' works fine for what is supposed to do. HOWEVER it does not work with absolutes paths. So consider the file structure:
include.js dir1
Resources
dir2 fileToIncludeIn.js
In fileToIncludeIn.js one cannot include the include.js file.
A general solution would be to have all your files in one directory. If someone can solve this problem please post.
P.S. speaking from iPhone perspective.
-
What version of Titanium are you using? this works fine for me here,
Ti.include('path/file.js');
will include file at resources/path/file.js
i can then do includes in file.js
Ti.include('anotherpath/file.js');
will include resources/anotherpath/file.js
or have i misunderstood the question?
-
I gave up trying to deal with this issue. But I notice that the KS has the same issue. Notice that all of there examples are in a single Dir.
What I have done is never go more than 1 level of dirs down. So I have dirs for TAB01, TAB02, TAB03, TAB05… TAB02-procA, TAB02-procB… PROC01, PROC02… RESOURCES/INCLUDE/InFile01.js
This way, all of the related code dirs tend to cluster together when listing for organization.
I can always use Ti.include('../INCLUDE/fileToInclude.js'); from any other file. I just never put JS files at root other than the app.js.
And this works really well for me. -
Anyone?
-
This works for me,
may have to change the amount of folder escapes for your situationvar fh = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'images/temp.js'); Ti.include('../../../../../../../../../../'+fh);
-
I gave up trying to deal with this issue. But I notice that the KS has the same issue. Notice that all of there examples are in a single Dir.
What I have done is never go more than 1 level of dirs down. So I have dirs for TAB01, TAB02, TAB03, TAB05… TAB02-procA, TAB02-procB… PROC01, PROC02… RESOURCES/INCLUDE/InFile01.js
This way, all of the related code dirs tend to cluster together when listing for organization.
I can always use Ti.include('../INCLUDE/fileToInclude.js'); from any other file. I just never put JS files at root other than the app.js.
And this works really well for me. -
Posted this in the other threads: https://appcelerator.lighthouseapp.com/projects/32238/tickets/2585-include-paths-for-iphone-and-android-are-not-based-on-the-same-starting-point
This issue has been resolved with 1.7:
http://builds.appcelerator.com.s3.amazonaws.com/index.html -
I gave up trying to deal with this issue. But I notice that the KS has the same issue. Notice that all of there examples are in a single Dir.
What I have done is never go more than 1 level of dirs down. So I have dirs for TAB01, TAB02, TAB03, TAB05… TAB02-procA, TAB02-procB… PROC01, PROC02… RESOURCES/INCLUDE/InFile01.js
This way, all of the related code dirs tend to cluster together when listing for organization.
I can always use Ti.include('../INCLUDE/fileToInclude.js'); from any other file. I just never put JS files at root other than the app.js.
And this works really well for me.