View is not updating from other controller Appcelerator Alloy

半腔热情 提交于 2019-12-11 13:31:47

问题


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();
});


来源:https://stackoverflow.com/questions/30951469/view-is-not-updating-from-other-controller-appcelerator-alloy

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