Titanium Community Questions & Answer Archive

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

Reusing js code

I'm working on an Android app. I have a tableview that I want to resuse in different windows in my app. So i figured I could make a separate js file for this tableview. I have tried the following test in the app.js:

var winX = Titanium.UI.createWindow({
    url : 'test.js'
});
var myview = Titanium.UI.createView({
    height:300,
    width:200,
    borderColor:'red',
    borderRadius : 10,
    backgroundColor : 'red'
});
myview.add(winX);
win1.add(myview);

The test.js file only contains a button, for test. This doesn't work, how can I solve the problem?

— asked October 16th 2010 by Jonas Pedersen
0 Comments

6 Answers

  • I have a couple comments, not sure how much they will help but I'll give it a shot.

    You usually need to call the open method of a window to make it display, I don't see that in this code so that may be why you see nothing. For example:

    winX.open();
    

    My next comment is that windows typically hold views, not the other way around so myview.add(winX); doesn't really make sense to me.

    Hope this helped.

    — answered October 16th 2010 by John McKnight
    permalink
    1 Comment
    • Hi, and thanks for the input. The following situation is not exactly mine, but exactly similar to it:

      Problem:

      • I have four tableViews tv1, tv2, tv3, tv4
      • I also have Two Tabs
      • In Tab One I need TWO tableViews, tv1 and tv2
      • In Tab Two I also need TWO tableViews, tv3 and tv4

      My failing idea:

      • Create TWO child windows in Each of the two root windows/tabs.
      • For Each child window, use the URL property to connect to one of the JS files with a tableView in it.

      Question:

      What is a good way to organize/access tableViews/JS-files in this (I assume quite normal) situation?
      Brgrds /JP

      — commented October 16th 2010 by Peter M
  • Also where is the TableView in your code?

    — answered October 16th 2010 by Duncan Kabinu
    permalink
    0 Comments
  • Also where is the TableView in your code?

    — answered October 16th 2010 by Duncan Kabinu
    permalink
    0 Comments
  • Also where is the TableView in your code?

    — answered October 16th 2010 by Duncan Kabinu
    permalink
    0 Comments
  • Hi, and thanks for the input. The following situation is not exactly mine, but exactly similar to it:

    Problem:

    • I have four tableViews tv1, tv2, tv3, tv4
    • I also have Two Tabs
    • In Tab One I need TWO tableViews, tv1 and tv2
    • In Tab Two I also need TWO tableViews, tv3 and tv4

    My failing idea:

    • Create TWO child windows in Each of the two root windows/tabs.
    • For Each child window, use the URL property to connect to one of the JS files with a tableView in it.

    Question:

    What is a good way to organize/access tableViews/JS-files in this (I assume quite normal) situation?

    Brgrds /JP

    — answered October 16th 2010 by Peter M
    permalink
    1 Comment
    • I have another project on my plate at the moment but later today I will attempt to answer this question.

      — commented October 16th 2010 by John McKnight
  • Guess this should Work…

    app window(in which ever you want to reuse the table…. just include the js file and on the open event of window add it to a view in the window)

    //include the test.js
    Ti.include('test.js');
    
    //app window
    var win=Ti.UI.createWindow({});
    
    //content View
    var Vu=Ti.UI.createView({});
    
    win.add(Vu);
    
    win.open();
    
    win.addEventListener('open',function(e){
        Vu.add(TbVu);
    });
    

    the test.js file

    //test.js
    
    var TbVu=Ti.UI.createTableView({
        data:data
    });
    
    — answered October 17th 2010 by Satta Ravi
    permalink
    0 Comments
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.