4

我试图弄清楚如何在合金视图之间传递参数。我将有一个有多个表的导航组,所以它可能有 3 到 5 层深。

我可以将参数从控制器传递到视图,但我想在单击时将信息(类别 ID)从视图传递到下一个表。

我不确定如何在合金中执行此操作,尝试访问变量时总是会出现未定义的错误。下面是我目前的设置。

在我看来,我有:index.xml、master.xml、row.xml、detail.xml、subDetail.xml

索引.xml

<Alloy>

    <Window id="index">
        <NavigationGroup id="navgroup">
            <Require src="master" id="master"/>
        </NavigationGroup>
    </Window>


</Alloy>

这是我的 index.js

Alloy.Globals.navgroup = $.navgroup;    


$.master.on('detail', function(e) {
    // get the detail controller and window references
    var controller = Alloy.createController('detail');
    var win = controller.getView();



    // open the detail windows 
    $.navgroup.open(win);
    /*
    if (OS_IOS && Alloy.isHandheld) {
        Alloy.Globals.navgroup.open(win);   
    } else if (OS_ANDROID) {
        win.open();
    }*/
});




$.index.open();

大师.xml

<Alloy>

    <Window title="Categories">
        <TableView id="table" onClick="openDetail">


        </TableView>
    </Window>


</Alloy>

大师.js

function openDetail(e) {
$.trigger('detail', e);
}


var data = [];

var sendit = Ti.Network.createHTTPClient({ 

                     onerror: function(e){ 

                           Ti.API.debug(e.error); 

                           alert('There was an error during the connection'); 

                     }, 

                  timeout:1000, 

              });                      

              //Here you have to change it for your local ip 


              sendit.open('GET', 'http://url.com/json.php?showCats=1');

              sendit.send(); 

              //Function to be called upon a successful response 

              sendit.onload = function(){ 

                     var json = JSON.parse(this.responseText); 

                     //var json = json.todo; 

                     //if the database is empty show an alert 

                     if(json.length == 0){ 
                        $.table.headerTitle = "The database row is empty"; 
                     }                      

                     //Emptying the data to refresh the view 



                     //Insert the JSON data to the table view


                    for ( var i=0; i<json.length; i++){ 


                        data.push(Alloy.createController('row', {                           
                            name: json[i].CatName,
                            catID: json[i].CatID

                        }).getView());

                        //data.push(row);

                        Ti.API.info(json[i].CatName);
                        Ti.API.info(json[i].CatID);


                    }                                     

                    $.table.setData(data);                      
               }; 

行.xml

<Alloy>
<TableViewRow>
<Label id="name"/>
<Label id="catID"/>
</TableViewRow>
</Alloy>

行.js

var args = arguments[0] || {};


$.row.fighterName = $.name.text = args.name;
$.catID.text = args.catID;

详细信息.xml

    <Alloy>

<Window title="Sub Categories">
<TableView id="subtable" onClick="openSubDetail">
</TableView>

</Window>
</Alloy>

细节.js

function openSubDetail(e) {
$.trigger('subDetail', e);


}


var data = [];



var sendit = Ti.Network.createHTTPClient({ 

                     onerror: function(e){ 

                           Ti.API.debug(e.error); 

                           alert('There was an error during the connection'); 

                     }, 

                  timeout:1000, 

              });                      

              //Here you have to change it for your local ip 



              Ti.API.info('Cat id');
              Ti.API.info(catID);
              Ti.API.info('data Value:'+ $.detail.catID );


              sendit.open('GET', 'http://url.com/mobile/includes/json.php?catID=12');

              sendit.send(); 

              //Function to be called upon a successful response 

              sendit.onload = function(){ 

                     var json = JSON.parse(this.responseText); 

                     //var json = json.todo; 

                     //if the database is empty show an alert 

                     if(json.length == 0){ 
                        $.table.headerTitle = "The database row is empty"; 
                     }                      

                     //Emptying the data to refresh the view 



                     //Insert the JSON data to the table view


                    for ( var i=0; i<json.length; i++){ 


                        data.push(Alloy.createController('subDetail', {                         
                            name: json[i].SubcatName,
                            catID: json[i].CatID

                        }).getView());

                        //data.push(row);
                        Ti.API.info('Second Level');
                        Ti.API.info(json[i].SubcatName);

                    }                                     

                    $.subtable.setData(data);                      
               }; 
4

1 回答 1

2

在行单击事件上创建一个新控制器并将对象作为参数传递给控制器​​。这样当在控制器中打开窗口时,控制器就有了数据。

subDetail它与创建行时所做的相同

于 2013-06-17T02:22:54.587 回答