Search code examples
javascriptfunctionparameter-passingtitaniumdom-events

Titanium Pass Textfield Value to New Window


I am attempting to take the value of a textfield and pass it to a new window (Javascript file) using Ti.App.fireEvent (http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.App-method-fireEvent) so that I can eventually insert it into a database when a button is pressed... So far I have a variable that stores the value of the textfield called usernameValue. This will then be sent to an http parameter value. There has to be something simple that I'm missing. Let me know if more information is needed to make this question more clear.

TexField where data originally comes from:

var username = Titanium.UI.createTextField({
    //color : '#ffffff',
    top : 10,
    width : 300,
    height : 45,
    hintText : 'Username',
    keyboardType : Titanium.UI.KEYBOARD_DEFAULT,
    returnKeyType : Titanium.UI.RETURNKEY_DEFAULT,
    borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
    clearButtonMode: Titanium.UI.INPUT_BUTTONMODE_ONFOCUS
});

Needs passed from login.js:

//FireEvent parameter passing!
var usernameValue = username.value;

Ti.App.addEventListener('sendDataToScreen', function()
{
    Ti.App.fireEvent('app:populateUsername', usernameValue);
});

IM.js retrieval:

//FireEvent Paremeter Passing!
Ti.App.fireEvent('app:populateUsername', usernameValue);

New Window (in case this helps):

//if logged in, open new window!
        var IM = Ti.UI.createWindow({
            url : 'IM.js',
            navBarHidden : false,
            // fullscreen:false

        });
        IM.open();

Button parameter passing where used:

//posting of IM onclick to database!!
postBtn.addEventListener('click', function(e) {

    if (chatBox.value != '') {

        createReq.open("POST", "http://***.php");
        var params = {

            post : chatBox.value,
            user : _data.username
        };
        createReq.send(params);

        //tap to repload and clear
        var rd = [];
        tableview.data = rd;
        setData();

    } else {
        var blankAlert = Titanium.UI.createAlertDialog({
            title : 'Posting Error',
            message : 'Post cannot be empty',
            buttonNames : ['OK']
        });
        blankAlert.show();
    }
});

Solution

  • when sending data

    Ti.App.fireEvent('app:populateUsername', { "username" : usernameValue });
    

    when receiving data

    Ti.App.addEventListener('app:populateUsername', function(_data)
    {
        // all the data passed
        Ti.API.info(JSON.stringify(_data));
    
        // the value is here
        Ti.API.info("the name passed as a parameter " + _data.username));
    });