Results 1 to 4 of 4

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

  1. #1
    Sencha User
    Join Date
    Jul 2011
    Posts
    4
    Vote Rating
    1
      1  

    Lightbulb 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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    In your init method in your controller:

    Code:
    this.control({
        'simplemain dataview' : {
            itemclick: function(dv, record, item, index, e, opts) {.....}
        }
    });
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha User
    Join Date
    Jul 2011
    Posts
    4
    Vote Rating
    1
      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
    506
    Answers
    26
    Vote Rating
    19
      0  

    Default

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

    Code:
      itemSelector: '.image400',

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •