1. #1
    Sencha User
    Join Date
    Nov 2010
    Posts
    395
    Vote Rating
    6
    gkatz is on a distinguished road

      0  

    Default get reference to 'parent object' from within a mixin

    get reference to 'parent object' from within a mixin


    Hi;
    I need to access a mixin 'parent object' from within the mixin. I though they share the same 'this' reference but it does not seem to be the case.
    how can I achieve this?
    thanks

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Vote Rating
    847
    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


    this should work:

    Code:
    Ext.define('MyMixin', {
        foo : function() {
            this.setHtml('this worked');
        }
    });
    
    Ext.define('MyView', {
        extend : 'Ext.Container',
        xtype  : 'myview',
    
        mixins : {
            mymixin : 'MyMixin'
        },
    
        initialize : function() {
            this.callParent();
    
            this.mixins.mymixin.foo.call(this);
        }
    });
    
    Ext.Viewport.add({
        xtype : 'myview'
    });
    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.

  3. #3
    Sencha User
    Join Date
    Nov 2010
    Posts
    395
    Vote Rating
    6
    gkatz is on a distinguished road

      0  

    Default


    thanks for the fast reply!
    what I was looking for is a way for the mixin to get a reference to its 'parent object' in case of mixin->parent object communication. for example: say the mixin has some callback function, that is called from the outside, and needs to call the parent object as a result.
    for now what I did it simply 'send' the parent object to the mixin by implementing a init(fatherCmp) function in the mixin that must be called form the parent object's initialize function in order for things to work.
    Is there another way? if not, I think mixins should get such an option built in the ST framework...
    thanks in advance.

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Vote Rating
    847
    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


    So you have a parent class (MyView from my example) and you want to execute a method on the MyMixin? The mixin can be accessed from this.mixins or things are placed onto the parent class. Observable is a mixing and it puts the fireEvent method on the parent class, the parent class doesn't have that method.

    If you want to fire a method on the parent class from the mixin, my example shows how to do this.

    So the mixin can reference the parent class via the scope and the parent class can reference the mixin either by the this.mixins object or the things the mixins place on the parent class.
    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.

Thread Participants: 1

Tags for this Thread