Class events

Topics: Language Specification
Nov 1, 2012 at 1:19 AM

Any plans to add support for class events, like C# for example?




class Field   { 

      event change(instance: Field, newValue : string, oldValue:string) ;


var field = new Field();

field.change += function (instance: Field, newValue: string, oldValue: string) { 

//could output this js

field.addEventListeners('change', function (instance: Field, newValue: string, oldValue: string) {


Nov 6, 2012 at 5:53 PM

Not currently planned.  There are so many different event patterns in JavaScript today, that it is hard to imagine a feature here that would be broadly useful. 

A potential future mixin system could make it easier to combine event source behavior into a class definition - but that is something that would be longer term.

Dec 5, 2012 at 1:27 PM

I'd like some kind on built in eventing system too.

I have converted our backbone.js project over to typescript but the events off of the models are still loose.

Is there a pattern I could follow to have Views classes subscribe to strongly typed events from a Model class in Typescript?


Dec 5, 2012 at 3:49 PM

If I recall correctly, the event listening/dispatching paradigm is pretty much based on a normal Observer design pattern. If you just want DOM-less event dispatching in TypeScript, I've already wrote a simple little library to do just that. You can find it on GitHub as EventDispatcher-ts. I've only ever used it on pure ts classes that weren't already inheriting from another base, but you should still be able to make it work if you do it right. I hope this helps!

John Vrbanac

Dec 5, 2012 at 6:58 PM

Awesome work John!

Yes, I'm looking for non-DOM events.  I have a single page app with many views and one model.

Views set data on the model, then I want other views to get notified (ideally in a strongly typed fashion) by the model.

I will take a look at your helpers there.  

Thanks much!