View is not updating from other controller Appcelerator Alloy
Asked Answered
A

1

1

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. 
Azikiwe answered 20/6, 2015 at 7:29 Comment(0)
R
0

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 Documentation.

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();
});
Roundy answered 28/6, 2015 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.