Titanium JavaScript: How to pass data/values from one window to another window

孤者浪人 提交于 2019-12-11 19:22:43

问题


In objective c we can pass data between two classes very easily by nextClassname.recievedVariable = passedVariable;

i tried same with titanium, but failed

I tried as follows

in Second Class

$.table.addEventListener('click', function(e) {
                         var selected = e.row;

                         alert(e.row.title);

                         var TodayStatus = Titanium.UI.createWindow({ url:'TodayStatus.js' });

                         TodayStatus.seletedProj = e.row.title;
                         // var TodayStatus = new Alloy.createController("TodayStatus");
                            TodayStatus.getView().open();


                         });

in the first Calss whic we have to recieve string from another class

var win = Ti.UI.currentWindow;
Ti.API.info(win.seletedProj);

But causes errors like

  message = "'undefined' is not an object (evaluating 'win.seletedProj')";
[ERROR] :      name = TypeError;

回答1:


You can pass the data by passing parameter like this.

x.addEVentListener('click', function(e){
    var controller = require('controllerPath').createWindow(e.value);
controller.open();
})

And in controller.js

exports.createWindow = function(value)
{
   //whatever You like to do with UI
}



回答2:


If you create a new window by using the 'url' parameter it automatically puts that code into it's own Sub-context and it isn't possible to pass complex objects accross, see here:

http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.UI.Window

I don't tend to do it this way anymore.

The way i would do it would be to create your todayStatus window as a common js class:

// todayStatus.js

var win = Ti.UI.createWindow({ top:0, left: 0, right: 0, bottom:0, etc... });

//any extra visual building code can go here

win.open();


exports.seletedProj = function(rowTtl){
    //this function is available outside the class
}

Then you can reference it from your main class like this:

// main.js

var TodayStatus = require('todayStatus');

TodayStatus.seletedProj(e.row.title);

etc...

Hope that helps




回答3:


see link here for how to do it in Alloy,

https://github.com/aaronksaunders/alloy_fugitive/blob/master/app/controllers/Fugitives.js#L29

but basic idea is to pass the object as a parameter when creating the new controller.

$.table.addEventListener('click', function(_e) {
    var detailController = Alloy.createController('FugitiveDetail', {
        parentTab : $.fugitiveTab,
        data : fugitiveCollection.get(_e.rowData.model)
    });
    $.fugitiveTab.open(detailController.getView());
});

The link I provided has a complete solution using Alloy



来源:https://stackoverflow.com/questions/18187554/titanium-javascript-how-to-pass-data-values-from-one-window-to-another-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!