mardi 27 janvier 2015

[Android native] Custom functions gone from views when pushed in a NavigationView

Hi guys, I'm facing a problem using a NavigationView, but only if I build with Phonegap and then run app in my Android device (it's a Samsung Galaxy S4 running Android 4.4.2): the same exact code works fine if I test it locally in Chrome. This is the problem: if I define a function in a view and push that view in the Navigation one, then I can't use my custom function because console says me that it's undefined. To clarify, and also because my app is lot more complicated, I've build up a very simple demo.

This is the code of the Main view (the one generated by sencha cmd tool, by the way I use the 5.1.0.26 version):


Code:



Ext.define('demo.view.Main', { extend: 'Ext.NavigationView',
xtype: 'main',
requires: [ 'Ext.dataview.List' ],
config: {
fullscreen: true,
itemId: 'mainPage',
items: [
{
title: "Demo",
layout: 'fit',
items: [
{
xtype: 'list',
itemTpl: '{text}',
data: [
{ text: 'Item 1' },
{ text: 'Item 2' },
{ text: 'Item 3' }
],
listeners: {
itemtap: function (list, index, item, record) {
var mainPage = list.up('#mainPage');
var subPage = demo.app.subPages[index];
console.log(subPage.greet);
mainPage.push(subPage);
}
}
}
]
}
]
}
});

An this is the code of the launch function of the app.js file:


Code:



launch: function() { // Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();

this.subPages = [
{
xtype: 'container',
title: "Page 1",
items: [
{
xtype: 'button',
text: 'Greet',
listeners: {
tap: function(btn){
btn.parent.greet();
}
}
}
],
greet: function(){
alert("Hi!");
}
},
{
xtype: 'container',
title: "Page 2",
items: [
{
xtype: 'button',
text: 'Greet',
listeners: {
tap: function(btn){
btn.parent.greet();
}
}
}
],
greet: function(){
alert("Hello!");
}
},
{
xtype: 'container',
title: "Page 3",
items: [
{
xtype: 'button',
text: 'Greet',
listeners: {
tap: function(btn){
btn.parent.greet();
}
}
}
],
greet: function(){
alert("Nice to meet you!");
}
}
],

// Initialize the main view
Ext.Viewport.add(Ext.create('demo.view.Main'));
}

The app starts and shows me the list with the 3 items: when I tap on one of them, in console I can see the body of my custom "greet" function (both in Chrome and Android). This, to be more precise:


Code:



function (){
alert("Hi!");
}

But if in Android, I click on one of the buttons that should call that function, this is the error that comes up:


Code:



01-27 19:33:55.568: I/chromium(13540): [INFO:CONSOLE(1)] "Uncaught TypeError: Object [object Object] has no method 'greet'", source: file:///android_asset/www/app.js (1)

Hope you guys can help me, thanks in advance.



[Android native] Custom functions gone from views when pushed in a NavigationView

Aucun commentaire:

Enregistrer un commentaire