如何在按钮点击钛上加载另一个js文件

时间:2013-07-25 05:47:34

标签: titanium titanium-mobile

我有App.js

(function() {       
    Window = require('ui/tablet/ApplicationWindow');

    }
    new Window().open();
    })();

从那里加载ApplicationWindow.jsApplicationWindow.js

function ApplicationWindow() {
    //load component dependencies
    var FirstView = require('ui/common/FirstView');

    //create component instance
    var self = Ti.UI.createWindow({
        backgroundColor:'#ffffff'
    });
    var win2 = Titanium.UI.createWindow({
    backgroundColor: 'red',
    title: 'Red Window'
    }); 

    //construct UI
    var firstView = new FirstView();
    var nav = Titanium.UI.iPhone.createNavigationGroup({
    window: win2
    });
    win2.add(firstView);
    self.add(nav);
    self.open();
    //self.add(firstView);
    if (Ti.Platform.osname === 'ipad') {
        self.orientationModes = [Ti.UI.LANDSCAPE_LEFT] ;
    };
    return self;
}

//make constructor function the public component interface
module.exports = ApplicationWindow;

我在FirstView.js中获得了一个包含2个文本字段和一个按钮登录的视图。该视图有一个标题为红色窗口的导航栏。我想在loginButton上加载Home.js。 这是loginButtonClick代码:

  loginButton.addEventListener ('click', function(e){
          //navigate to Home.js
      }); 

我怎么能这样做。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:2)

在当前文件中尝试以下方法

loginButton.addEventListener('click', function(e){
    var win = Ti.UI.createWindow({
        backgroundColor : 'white',
        url             : 'home.js' //Path to your js file
    });
    win.open();
});

<强> home.js

var myWin = Ti.UI.currentWindow;
//You can add your controls here and do your stuff.
// Note that never try to open myWin in this page since you've already opened this window

您还可以尝试以下方法

方法2

//In your current page
loginbutton.addEventListener('click', function(e) {
    var Home = require('/ui/common/Home');
    var homePage = new Home();
    homePage.open();
});

您的Home.js文件

function Home() {
    var self = Ti.UI.createWindow({
        layout : 'vertical',
        backgroundColor:'white'
    });
    //Do your stuff here
    //Add other controls here

    return self;
}
module.exports = Home;

查看this answer,它也与您的问题相同