ggutenberg
ggutenberg

Reputation: 7370

Event listener on a Sencha Touch generated setter

Is it possible to set an event listener to trigger when a user-defined config property is changed? Ex.

Ext.define('myClass', {
    singleton: true,
    config: {
        myProperty: null
    }
}

And I want to set up a listener so that when I call myClass.setMyProperty('blah'); it gets triggered. Possible?

Upvotes: 1

Views: 456

Answers (2)

programmer_subself
programmer_subself

Reputation: 33

The comments in the accepted answer actually contain the full answer, so for the sake of googlers who get here, I'll type it up:

The answer is to create a function called applyX or updateX (where X is your capitalized property), depending on if you want to receive the new value before or after (respectively) it is physically changed in the config. Sencha will call your function:

Ext.define('myClass', {
    singleton: true,
    config: {
        myProperty: null
    },
    updateMyProperty: function(value) {
        ... do something with value. this.getMyProperty() will also work
    } 
}

Read more here: http://docs.sencha.com/touch/2.4/core_concepts/class_system.html

Upvotes: 0

Akatum
Akatum

Reputation: 4016

Generated setters does not fire any event.

However you can define your own setter and fire your custom event from it. Your class have to use Ext.mixin.Observable mixin (or extend from some class which use this mixin) if you want to fire events from it.

Ext.define('myClass', {
    mixins: ['Ext.mixin.Observable'],
    singleton: true,
    config: {
        myProperty: null
    },
    setMyProperty: function(value) {
       this.myProperty = value;
       this.fireEvent('myPropertySetted');
    }
}

Upvotes: 1

Related Questions