amurcia
amurcia

Reputation: 791

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

Upvotes: 0

Views: 604

Answers (2)

Thomas Lemaitre
Thomas Lemaitre

Reputation: 1154

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

Upvotes: 2

HamidMly
HamidMly

Reputation: 42

you have to fire event in the controller not in the view, try this :

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

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

Upvotes: -1

Related Questions