antun
9 Oct 2012, 4:11 PM
I'm very new to Sencha, so I don't know if I'm running into a bug here or doing something wrong. I want to define a method (function) on a view, and call that function from a controller. I assumed that I was supposed to use Controller.getView() (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.app.Controller-method-getView) to get a reference to the view, but it doesn't appear to work.
Here's my view code:
Ext.define('SFC.view.flashcard.Show' ,{
extend: 'Ext.container.Container',
alias : 'widget.flashcardshow',
layout: 'fit',
title : 'Flashcard TODO',
store: 'SpanishWords',
items : [
{
itemId: 'flashWord',
xtype: 'panel',
layout: 'fit',
title: 'Spanish Flashcards',
html: '<div style="display: table-cell; vertical-align: middle;">palabra</div>',
bodyStyle: 'text-align: center; display: table; font-size: 96px; background:#ffc;',
tools: [
{
xtype: 'button',
action: 'next',
text: 'Change word',
scale: 'large'
}
]
}
],
initComponent: function() {
this.callParent(arguments);
},
updateWord: function(newWord) {
// Function I'm trying to call
console.log("updateWord", newWord);
}
});
Here's my controller code:
Ext.define('SFC.controller.Flashcards', {
extend: 'Ext.app.Controller',
views: [
'flashcard.Show'
],
stores: [
'SpanishWords'
],
init: function() {
console.log('Initialized Flashcards! This happens before the Application launch function is called');
this.control({
'viewport > panel': {
render: this.onPanelRendered
},
'button[action=next]': {
click: this.doNextWord
}
});
},
onPanelRendered: function() {
console.log('The panel was rendered');
},
doNextWord: function() {
var words = Ext.data.StoreManager.getByKey("SpanishWords");
var i = Math.round(Math.random() * (words.getCount()-1));
var nextWord = Ext.data.StoreManager.getByKey("SpanishWords").getAt(i).get("word");
var v = this.getView('flashcard.Show');
console.log(v);
v.updateWord("blah");
}
});
The getView('flashcard.Show') does appear to return something, but I can't seem to call updateWord on it. The reason I can tell that it is returning something is that fi I change the viewName to something that is not specified in the controller (e.g. "foo") the result is undefined.
The console.log(v) line outputs the following in Chrome. (Like I said, I'm new to Sencha, so any tips on how to serialize this output into something more specific would be appreciated. I searched, but couldn't find any way to serialize an object to provide more useful debug info):
function constructor() { return this.constructor.apply(this, arguments) || null; }
I'm using ext-4.1.1a.
Thanks in advance for any help!
-Antun
Here's my view code:
Ext.define('SFC.view.flashcard.Show' ,{
extend: 'Ext.container.Container',
alias : 'widget.flashcardshow',
layout: 'fit',
title : 'Flashcard TODO',
store: 'SpanishWords',
items : [
{
itemId: 'flashWord',
xtype: 'panel',
layout: 'fit',
title: 'Spanish Flashcards',
html: '<div style="display: table-cell; vertical-align: middle;">palabra</div>',
bodyStyle: 'text-align: center; display: table; font-size: 96px; background:#ffc;',
tools: [
{
xtype: 'button',
action: 'next',
text: 'Change word',
scale: 'large'
}
]
}
],
initComponent: function() {
this.callParent(arguments);
},
updateWord: function(newWord) {
// Function I'm trying to call
console.log("updateWord", newWord);
}
});
Here's my controller code:
Ext.define('SFC.controller.Flashcards', {
extend: 'Ext.app.Controller',
views: [
'flashcard.Show'
],
stores: [
'SpanishWords'
],
init: function() {
console.log('Initialized Flashcards! This happens before the Application launch function is called');
this.control({
'viewport > panel': {
render: this.onPanelRendered
},
'button[action=next]': {
click: this.doNextWord
}
});
},
onPanelRendered: function() {
console.log('The panel was rendered');
},
doNextWord: function() {
var words = Ext.data.StoreManager.getByKey("SpanishWords");
var i = Math.round(Math.random() * (words.getCount()-1));
var nextWord = Ext.data.StoreManager.getByKey("SpanishWords").getAt(i).get("word");
var v = this.getView('flashcard.Show');
console.log(v);
v.updateWord("blah");
}
});
The getView('flashcard.Show') does appear to return something, but I can't seem to call updateWord on it. The reason I can tell that it is returning something is that fi I change the viewName to something that is not specified in the controller (e.g. "foo") the result is undefined.
The console.log(v) line outputs the following in Chrome. (Like I said, I'm new to Sencha, so any tips on how to serialize this output into something more specific would be appreciated. I searched, but couldn't find any way to serialize an object to provide more useful debug info):
function constructor() { return this.constructor.apply(this, arguments) || null; }
I'm using ext-4.1.1a.
Thanks in advance for any help!
-Antun