Strong typed events, how to?

Topics: General
Oct 23, 2012 at 8:57 PM

Hi, 

I see this pattern in some libraries for example a changed event in kendo

calendar.bind("changed", function(e){... });

I would like to change this to something typed without writing a wrapper for calendar class...

so that i can have some intellisense for the supported events.

Any ideas how to do this?

 


 

 

 

 

Oct 25, 2012 at 4:09 PM

In case you are just looking for intellisense within the callback method, you could use something simple like this:

 

calendar.bind("changed", function(e:ChangedEvent){... });

 

Of course you would have to declare the ChangedEvent interface.

--Peter

Oct 25, 2012 at 5:49 PM
Edited Oct 25, 2012 at 5:50 PM

You could define an interface for that calendar something like this 

 

interface Calendar {

bind((ChangeEvent) => void) ;

 

Then if you cast your 'calendar' variable to that interface, you would get all the right intelliintellisense. 

Oct 25, 2012 at 6:47 PM

Declare a class with static string fields for the events to use like an enumeration?

class CalendarEvents {
public static Changed = "changed";
}

calendar.bind(CalendarEvents.Changed, function (e) { });

There are supposed to be enums in the final version of TypeScript, which will probably address this need better.

On Oct 23, 2012 3:57 PM, "ursuletzu" <notifications@codeplex.com> wrote:

From: ursuletzu

Hi,

I see this pattern in some libraries for example a changed event in kendo

calendar.bind("changed", function(e){... });

I would like to change this to something typed without writing a wrapper for calendar class...

so that i can have some intellisense for the supported events.

Any ideas how to do this?


Read the full discussion online.

To add a post to this discussion, reply to this email (typescript@discussions.codeplex.com)

To start a new discussion for this project, email typescript@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Nov 1, 2012 at 7:56 PM

 You can also define an enum using a 'var' which will generate less output code then using a 'class':

var CalendarEvents = {
     Changed: "changed"
};

calendar.bind(CalendarEvents.Changed, function (e) { });

Nov 2, 2012 at 9:19 PM

signals are a more type-safe version of events

http://millermedeiros.github.com/js-signals/

Nov 7, 2012 at 10:59 AM

The js-signals stuff is every similar to how I've been approaching events in TypeScript but to be honest it doesn't even need to be as complex as they've made it.  I just posted the basic code I've been using to strongly type events in TypeScript and the core dispatcher boils down to about 40 lines of JavaScript and with the way I've done it all of the actual typing stuff boils out at compile time yet you get an amazing tooling experience.  It doesn't help much with existing frameworks like jQuery but for new TypeScript classes I hope it's helpful:

http://typescript.codeplex.com/discussions/402228

Nov 7, 2012 at 12:31 PM

I was also using js-signals in my code, but later on I discovered the 'jQuery.Callbacks( flags )' function which does the same thing. So don't use js-signals if you're already using jQuery.

The jquery.d.ts file will guide you, but contains a small bug. The flags argument is optional, so the jquery.d.ts file should contain:

/*********
CALLBACKS
**********/
Callbacks(flags?: any): JQueryCallback;

Instead of:

/*********
CALLBACKS
**********/
Callbacks(flags: any): JQueryCallback;