View is not updating from other controller Appcele

2019-08-12 04:02发布

问题:

I hope you will be fine. I am having trouble updating views in Titanium Appcelerator Alloy,

I basically want to be able delete previous children from picker and then add new one in picker that is in a different controller/view that i am currently in.....

I have followed this THIS SOLUTION unfortunately that is not working for me. Here is the code I am trying.

createEvent.js

Ti.App.addEventListener('db_update', function(){
   alert("OK");
   $.picker.removeAllChildren();
})

customParty.js

$.btnclick.addEventListener('click', function(){
    Ti.App.fireEvent('db_update');
});
 // OK alert shows up but the children of picker aren't removed. 

回答1:

Since Ok Alert is shown, you are in the good way and the callback function is called successfully. The problem here is that calling removeAllChildren method is not removing rows from your picker. the solution is to iterate over colums and delete rows like this :

Ti.App.addEventListener('db_update', function(){
   alert("OK");
   //get picker columns
   var columns=$.picker.getColumns();
   //Iterate over picker columns
   for (var it=0,length=columns.length;i<length;it++){
        //iterate over column rows
        if(columns[it]){
             var len = col.rowCount;
             for(var index=0,collength=columns[it].length;index<collength;index++){
                //remove rows[index] of columns[it]
                columns[it].removeRow(columns[it].rows[index]);
             }
        }
   }
});

By the way Applcelerator's folks said that using global events (Ti.App events) may cause problems in memory managements...

Keep in mind that app-level events are global, which means they remain in context the entire time your app is running (unless you remove them). This also means that any objects they reference will also remain in scope while your app runs. This could prevent those objects from being garbage collected.Appcelerator gays.

Another method is to use global functions:

In your first view controller (where picker is defined):

Alloy.Globals.removeAllPickerChildren=function(){
      //do what you want here
};

then in the seconde viewcontroller:

$.btnclick.addEventListener('click', function(){
    if(Alloy.Globals.removeAllPickerChildren)
         Alloy.Globals.removeAllPickerChildren();
});