1. #1
    Sencha User
    Join Date
    Jul 2011
    Posts
    4
    Vote Rating
    1
    extjs knight is on a distinguished road

      1  

    Lightbulb Answered: MVC structure, XTemplate, Controllers, and event Listeners

    Answered: MVC structure, XTemplate, Controllers, and event Listeners


    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

    Code:
    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

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

  2. In your init method in your controller:

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

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    35,704
    Answers
    3356
    Vote Rating
    750
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    In your init method in your controller:

    Code:
    this.control({
        'simplemain dataview' : {
            itemclick: function(dv, record, item, index, e, opts) {.....}
        }
    });
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Jul 2011
    Posts
    4
    Vote Rating
    1
    extjs knight is on a distinguished road

      0  

    Default


    The listener isn't firing when I try attaching it via this method.

  5. #4
    Sencha User mberrie's Avatar
    Join Date
    Feb 2011
    Location
    Bangkok, Thailand
    Posts
    507
    Answers
    26
    Vote Rating
    14
    mberrie will become famous soon enough mberrie will become famous soon enough

      0  

    Default


    Your itemSelector config is missing the dot (it should be a valid CSS selector, not a CSS class).

    Code:
      itemSelector: '.image400',

Thread Participants: 2

Tags for this Thread

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar