Typescript: Notify when property changed

Hello,

I wrote the following code:

@property ({ type: cc.SpriteAtlas })
_cellsAtlas: cc.SpriteAtlas = null;

And then, I added get and set like this:

@property({ type: cc.SpriteAtlas })
get CellsAtlas() {
    return this._cellsAtlas;
}

set CellsAtlas(value) {
    this._cellsAtlas = value;
    // On Property Changed...
}

Is there a better way to do that? Less code? Better solution? (I am sure something is messed up here)
I am ending up with lots lines of code just for making props and I did not even started to scratch the surface…
The program runs fine and in JS it looks much cleaner.

Btw, What are the properties that I can add inside:
@property({ //here…? })
Any documentation for that?

My Cocos Creator version is 2.0.5 (currently the latest).

here is not way to use “notify” func in ts.because it is conflicted with ts syntax of ES6 Classes.
in ts syntax ,your code is the best way to realize the setter

@property ({ type: cc.SpriteAtlas })
_cellsAtlas: cc.SpriteAtlas = null;
@property({ type: cc.SpriteAtlas })
get CellsAtlas() {
    return this._cellsAtlas;
}

set CellsAtlas(value) {
    this._cellsAtlas = value;
    // On Property Changed...
}
1 Like

Thank you…

I also find this. Helped me with property types…

http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/class.html