PDA

View Full Version : MVC structure, XTemplate, Controllers, and event Listeners



extjs knight
12 Aug 2011, 2:00 PM
Hello all,

tl;dr - How can I rewrite my view, so that the XTemplate listeners reside in the controller, not the view?

I am working on a small application which allows the user to click on an image thumbnail to see a larger version of the image. The application is very basic and set up in MVC style. The layout uses border style with the large image being the center, and the thumbnails being south. The HTML for the center and the south is populated by XTemplate, which in turn is of course being populated by a call to a store which consumes JSON data from the server.

I have the application working correctly, but I had to break MVC concepts in order to get it to work. Specifically, when the view is rendering the thumbnails, I need it to attach a listener to each thumbnail, which contains a function that looks up the appropriate large image URL in the store, then sets the source of the main image to that image. I did this by writing the listeners into the view for the thumbnails, which breaks the spirit of MVC - the listener (or, at least, the function to fire?) should be in the controller, not the view..

Here's my main view, which I haven't yet broken up yet

/app/view/simple/Main.js

Ext.define('WHEE.view.simple.Main' ,{
extend: 'Ext.panel.Panel',
alias : 'widget.simplemain',
width:400,
layout: 'border',

initComponent: function() {
Ext.apply(this, {
items: [{
xtype: 'dataview',
store : 'Images',
itemSelector: 'image400',
region: 'center',
width:400,

tpl:
new Ext.XTemplate(
'<tpl for=".">',
'<tpl if="this.isMainImg(imageType)"',
'<div class = "image400">',
'<img id = "imagemain" src = "{imageURL}" width = "{imageSize}" height = "{imageSize}">',
'</div>',
'</tpl>',
'</tpl>',
{
isMainImg: function(imageType) {
return imageType == 'main';
}
}
)
},{

xtype: 'dataview',
store: 'Images',
itemSelector: 'imagethumb',
region :'south',
height:300,
listeners:
{
el:
{
click: function fn(e,t) {
thumbid = t.id.substring(5);
console.log('store - '+Ext.getStore('Images'));
console.log('thumbid - '+thumbid);
parentid = Ext.getStore('Images').getAt(thumbid).get('imageParent');
console.log('parentid - '+parentid);
parentURL = Ext.getStore('Images').getAt(parentid).get('imageURL');
console.log('parenturl - '+parentURL);
Ext.get("imagemain").dom.src = parentURL;
//console.log('clicked - '+t.id);
//console.log(arguments);
}
}
},
tpl:
new Ext.XTemplate(
'<tpl for=".">',
'<tpl if="this.isThumb(imageType)"',
'<div class = "imagethumb">',
'<img class = "thumbpic" id = "thumb{id}" src = "{imageURL}" width = "{imageSize}" height = "{imageSize}">',
'</div>',
'</tpl>',
'</tpl>',
{
isThumb: function(imageType) {
return imageType == 'thumb' || imageType == 'altthumb';
}
}
),

}]
});
this.callParent(arguments);
}


//html: '<div id = "image400"></div>',


});

Here's my only controller

/app/controller/Item.js


Ext.define('WHEE.controller.Item', {
extend: 'Ext.app.Controller',

views: ['simple.Main'],

stores: ['Images'],

models: ['Image'],

refs: [
{
ref: 'simpleview',
selector: 'simplemain'
}
],

init: function() {

console.log('Initialized Item! This happens before the Application launch function is called');
},



setupItem: function(SKU) {
var view = Ext.widget('simplemain');
this.getImagesStore().load({params:{"SKU":SKU}});

//console.log(Ext.get("thumb1"));
//console.log(this.getImagesStore());
//console.log(this.getImagesStore().getstoreId());
}

});

mitchellsimoens
13 Aug 2011, 9:06 AM
In your init method in your controller:


this.control({
'simplemain dataview' : {
itemclick: function(dv, record, item, index, e, opts) {.....}
}
});

extjs knight
15 Aug 2011, 11:41 AM
The listener isn't firing when I try attaching it via this method.

mberrie
16 Aug 2011, 4:24 AM
Your itemSelector config is missing the dot (it should be a valid CSS selector, not a CSS class).



itemSelector: '.image400',