Search code examples
titaniumappceleratorappcelerator-alloy

Titanium - Trigger from controller to a view


I have a widget.js with a table and in his headerView I have a controller view. The code:

var table = Ti.UI.createTableView({
      id: ('table' + i),
      data: tableData,
      separatorStyle: OS_ANDROID ? Ti.UI.TABLE_VIEW_SEPARATOR_STYLE_NONE : Ti.UI.iPhone.TableViewSeparatorStyle.NONE,
      headerView: Alloy.createController("Principal/Componentes/LocalizacionRow", {parent: $.board}).getView()
    });

I want to pass an event from LocalizacionRow to widget.js using $.trigger but it doesn't work.

My code:

LocalizacionRow.js

if(!e.cancel){
            $.localizacion.text = e.data[0].value;


            $.trigger('recargar');

            Ti.API.info("## Periódico: " +  Ti.App.Properties.getString('media') + " " + $.localizacion.text);
        }

widget.js

var header = table.getHeaderView();
header.on('recargar', function(e){
    Ti.API.debug("--- Recargar");
});

"--- Recargar" is never showed. What am I doing bad?

I've seen this code here: http://www.tidev.io/2014/09/10/the-case-against-ti-app-fireevent-2/

And here: https://wiki.appcelerator.org/display/guides2/Controller+events


Solution

  • If you want receive the event, you must add a trigger to the LocalizacionRow controller, like this :

    var LocalizacionRowController = Alloy.createController("Principal/Componentes/LocalizacionRow", {parent: $.board});
    
    LocalizacionRowController.on('recargar', function(e){
        Ti.API.debug("--- Recargar");
    });
    
    var table = Ti.UI.createTableView({
      id: ('table' + i),
      data: tableData,
      separatorStyle: OS_ANDROID ? Ti.UI.TABLE_VIEW_SEPARATOR_STYLE_NONE : Ti.UI.iPhone.TableViewSeparatorStyle.NONE,
      headerView: LocalizacionRowController.getView()
    });