Results 1 to 4 of 4

Thread: How does a mixin reference another one of its method from within itself?

  1. #1
    Sencha User Cisco Kid's Avatar
    Join Date
    Apr 2017
    Location
    San Jose, CA
    Posts
    4

    Default Answered: How does a mixin reference another one of its method from within itself?

    I want MyHelperMixin.otherMethod() to be able to call MyHelperMixin.firstMethod() without calling from MyView because MyOtherView is also using the same mixin and otherMethod will not know whether it is being called from MyView or MyOtherView.

    Code:
    Ext.define('MyHelperMixin', {
        firstMethod: function () {
            this.setHtml('this worked');
        },
    
        otherMethod: function () {
            // I want to call firstMethod() from here without going through MyView or MyOtherView 
            // (or if I do call through them, it would need to be generically, such that it could be from any view)
            // because various other views use MyHelperMixin as a helper class
        },
    });
    
    
    Ext.define('MyView', {
        extend : 'Ext.Container',
        mixins: [
            'myMixins/MyHelperMixin',
        ],
        xtype: 'myview',
    
    
        initialize : function() {
            this.callParent();
        }
    });
    
    
    Ext.define('MyOtherView', {
        extend : 'Ext.Container',
        mixins: [
            'myMixins/MyHelperMixin',
        ],
        xtype: 'myview',
    
    
        initialize : function() {
            this.callParent();
        }
    });

  2. Specify the scope along with the handler:

    Code:
    {
        handler: me.cloneHandler,
        scope: me
    }
    Which means that the button handler will run in the context of 'MyView'.

  3. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,239
    Answers
    757

    Default

    Those methods just get mixed into the class, so they act as class methods. Just call it directly "this.firstMethod()"
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  4. #3
    Sencha User Cisco Kid's Avatar
    Join Date
    Apr 2017
    Location
    San Jose, CA
    Posts
    4

    Default

    Sorry, I did not do a sufficient job with my post - more specifically, the reason I cannot use 'this' inside my helper method is because the shared method is actually a button handler, so this references the button component, NOT the page component like below. I want to put limitedUniqueId outside cloneHandler (like firstUniqueId), but I do not know how to reference it from within cloneHandler.

    Code:
    Ext.define('MyHelperMixin', {
    
        firstUniqueId: function (cfg) {
             var j = 0;
             var num = j;
             var newItmId, i, matched;
             var clones = cfg.clones;
             var cloneCt = clones.length;
    
             // this loop ensures that the new itemId does not already exist 
             do {
                 matched = false;
                 num++;
                 newItmId = cfg.id + '-' + num;
                 for (i = j; i < cloneCt; i++) {
                     if (clones[i].itemId === newItmId) {
                         matched = true;
                         // if match is at the front, skip it next time...
                         if (i === j) {
                             j++;
                         };
                         // we found a match, no need to look anymore...
                         break;
                     }
                 }
             } while (matched);// loop until the item[i].itemId does not match
    
             return num;  
        },
    
        cloneHandler: function (btn) {
            // I want to call firstUniqueId() from here without going through MyView or MyOtherView 
            // (or if I do call through them, it would need to be generically, such that it could be from any view)
            // because various other views use MyHelperMixin as a helper class
    
            var cfg = btn.handlerConfig;
            var xtype = cfg.prefix + '-' + cfg.id'
            var itemToClone = btn.up(xtype);
            var container = itemToClone.up();
            var items = container.query(xtype);
            // I want to call firstUniqueId() above (outside this method), NOT limitedUniqueId below...
            var uniqueId = limitedUniqueId({
                    id: cfg.id,
                    clones: items
                });
    
            function limitedUniqueId(cfg) {
                // same logic as firstUniqueId() above
                return num;            
            }
        },
    });
    
    Ext.define('MyView', {
        extend : 'Ext.Container',
        mixins: [
            'myMixins/MyHelperMixin',
        ],
        xtype: 'myview',
    
        initComponent: function() {
            var me = this;
    
            me.items = [{            itemId: 'add-btn',
                handlerConfig: {
                    prefix: 'someName',
                    id: 'myId',
                },
                handler: me.cloneHandler
            }];
    
            me.callParent(arguments);
        },
    });
    
    Ext.define('MyOtherView', {
        extend : 'Ext.Container',
        mixins: [
            'myMixins/MyHelperMixin',
        ],
        xtype: 'myview',
    
        initComponent: function() {
            var me = this;
    
            me.items = [{
                itemId: 'clone-btn',
                handlerConfig: {
                    prefix: 'anotherName',
                    id: 'anotherId',
                },
                handler: me.cloneHandler
            }];
    
            me.callParent(arguments);
        },
    });
    

  5. #4
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,239
    Answers
    757

    Default

    Specify the scope along with the handler:

    Code:
    {
        handler: me.cloneHandler,
        scope: me
    }
    Which means that the button handler will run in the context of 'MyView'.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

Similar Threads

  1. Object config in mixin will be reference by all class
    By joshuaavalon in forum Ext JS 6.x Q&A
    Replies: 1
    Last Post: 25 Feb 2019, 1:06 AM
  2. Replies: 1
    Last Post: 29 Mar 2014, 1:06 AM
  3. get reference to 'parent object' from within a mixin
    By gkatz in forum Sencha Touch 2.x: Discussion
    Replies: 3
    Last Post: 21 Dec 2012, 9:59 AM

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
  •