Getting typed JSon in TypeScript using Weld

1. juni 2013 20:32 by admin in .net, DRY, jQuery, MVC, typescript, Weld
Imagine you have a MVC controller exposing a Person as following: 

public JsonResult GetPerson(int id)
{
return Json(new Person
{
First = "Barack",
Last = "Obama"
},JsonRequestBehavior.AllowGet);
}
Now I want to use this client side. I want to update my "first" span with the First property of the person I get back from the server. After messing around I came up with the following: 
$(document).ready(function() {
$.getJSON("/Person/GetPerson/", { id: 4 }, function(data) {
$("#first").text(data.First);
});
});
This took me five minutes to get right (which JQuery method? -> Google, how to pass data? -> find example, plus a typo in the first/First member). Five minutes of stupid getting right what should be trivial. Why not let the compiler help? So using Weld I change my server side method to this :
[AjaxMethod]
public JsonResult<Person> GetPerson(int id)
{
return new JsonResult<Person>(Json(new Person
{
First = "Barack",
Last = "Obama"
}, JsonRequestBehavior.AllowGet)); ;
}
Basically I wrap my JsonResult in a generic wrapper JSonResult<T> (included with Weld) so that Weld knows what type to expect. Next I add the AjaxMethod Weld attribute. Client side I use TypeScript and use the generated proxy class.  
/// <reference path="Weld/PersonController.ts" />/// <reference path="typings/jquery/jquery.d.ts" />
$(document).ready(function () {
PersonController.prototype.GetPerson(2, (person) => {
$("#first").text(person.First)
});
});
Now person is a JavaScript object or a TypeScript 'any' type. No, Person is a fully defined TypeScript interface with a First and Last member just like my C# class. Here's the generated code:
/// <reference path="../typings/jquery/jquery.d.ts" />
class PersonController
{
GetPerson(id: number, callback: (data: Person) => any)
{
var url = "/Person/GetPerson";var data = { id: id };
$.ajax({
url: url,
type : "POST",
data: data,
success: callback,
});
}
}
interface Person
{
First: string;
Last: string;
}
As always in the end there is still JavaScript code generated which you can easily debug. Weld code is simple and clean. Easy huh? And there are more benefits:
  1. If I remove or rename my action-method the TypeScript compiler will detect my breaking client side code at compile time. If you just use javascript you will (hopefully) find out at runtime.
  2. If I add or remove properties from my Person class my changes will cascade to the clientside immediately. 
  3. I get a nice interface clientside making code completion really work 
  4. No more Googling JQuery methods
  5. This is more DRY (Don't Repeat Yourself). For example if you don't like JQuery you can build a Weld template using a different method. Regenate and you are ready
Weld is fully open source and can be found at weld.codeplex.com

wagner 12-7-2013 20:01:13 #
wagner

it's wrong

Merlyn Namanworth 31-8-2013 19:47:52 United States #
Merlyn Namanworth

I have recently started a web site, the info you offer on this website has helped me tremendously. Thank you for all of your time & work.

Laila Lacorte 3-9-2013 16:53:18 United States #
Laila Lacorte

At least Obama is asking Congess for approval before he strikes in the latest round. I understand that may not be what you are talking about but It is certainly relevant.

Roxann Wallack 11-9-2013 15:25:24 United States #
Roxann Wallack

Has anybody had a positive experience using the Fullerton Drake Medical center Weight Reduction program?

Copper Jewelry 4-10-2013 03:48:26 United States #
Copper Jewelry

Hello! I�ve been reading your web internet site for a even though now and finally got the courage to go ahead and give you a shout out from  Kingwood Texas! Just wanted to say keep up the good work!

Handmade Jewelry 2-11-2013 01:58:51 United States #
Handmade Jewelry

Billiard is really a game which is mostly played by the high class men and women

Handmade Jewelry 6-11-2013 06:15:55 United States #
Handmade Jewelry

New to your blog. Stumbled upon it browsing the internet. Keep up the excellent work. I'm hoping you update it regularly.

Handmade Jewelry 11-11-2013 03:15:03 United States #
Handmade Jewelry

Seriously, what a blog! I mean, you just have a fantastic deal of guts to go forward and tell it like it truly is. You happen to be what running a weblog need to have, an open minded superhero who isn’t frightened to tell it like it's. This really is certainly something people require to be up on. Good luck inside the foreseeable future, man.

My Favorite 12-11-2013 08:05:35 United States #
My Favorite

I need to to take the time and write to you that you have certainly helped us simply by creating this amazing posts. I was happy to follow your own subsequently helpful facts and put it to some fantastic use. I would really like to convey my gratitude and would like to tell your followers they are so fortunate to have someone like you creating fantastic material. I anticipate studying much more useful content like this from you down the road. Once more, thank you so much so much with the extremely helpful post. We have added your rss feed as well as book-marked you web site in my social bookmarking web site. Cheers!

call center jobs 13-11-2013 12:46:42 United States #
call center jobs

bonjour je suis un �tudiante de la portugal j'ai trop aimer ton site puis les couleurs extraordinairement sur l'article la site . R�ellement merci pour l'gouverneur de ce online site

call center job postings 13-11-2013 12:51:40 United States #
call center job postings

bonjour je suis un petit �tudiante de la france j'ai trop aimer votre implacable web page ainsi les colorations extraordinairement sur l'article la site . Vraiment merci pour l'dirigeant fuente ce blog

Santa letters 4-12-2013 17:41:26 United States #
Santa letters

My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he's tryiong none the less. I've been using Movable-type on various websites for about a year and am concerned about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any help would be greatly appreciated!

Letter From Santa 6-12-2013 07:48:33 United States #
Letter From Santa

Hey there! Quick question that's entirely off topic. Do you know how to make your site mobile friendly? My blog looks weird when viewing from my iphone 4. I'm trying to find a theme or plugin that might be able to resolve this problem. If you have any recommendations, please share. Appreciate it!

Buddy Dinnendahl 14-12-2013 21:18:13 United States #
Buddy Dinnendahl

Simply want to say your article is as amazing. The clearness for your put up is simply nice and that i could think you're knowledgeable on this subject. Well with your permission allow me to grab your feed to stay up to date with drawing close post. Thanks a million and please continue the enjoyable work.  <a href=http://acne.adsuse.com>how to remove pimples</a>

Xavier Koda 15-12-2013 06:26:11 United States #
Xavier Koda

Nice post. I used to be checking constantly this weblog and I am impressed! Extremely useful information specifically the final part Smile I maintain such information a lot. I was looking for this particular information for a long time. Thanks and best of luck.   <a href=http://asthma.adsuse.com>asthmatic bronchitis</a>

Darell Tretina 15-12-2013 16:27:44 United States #
Darell Tretina

Hello, I log on to your blog regularly. Your humoristic style is awesome, keep doing what you're doing!  <a href=http://cellulite.adsboards.com>how to get rid of cellulite</a>

Felix Roton 15-12-2013 21:10:38 United States #
Felix Roton

Hello, yeah this article is truly fastidious and I have learned lot of things from it about blogging. thanks.  <a href=http://creditrepair.usabestads.com>credit repair</a>

Gregg Doubet 16-12-2013 02:22:21 United States #
Gregg Doubet

I got this website from my buddy who told me regarding this site and at the moment this time I am visiting this site and reading very informative content at this time.  <a href=http://cancer.adsuse.com>cancer treatment</a>

ATT UVERSE 20-12-2013 02:49:22 United States #
ATT UVERSE

Valuable information. Lucky me I found your web site unintentionally, and I'm shocked why this twist of fate did not came about in advance! I bookmarked it.

Willena 24-12-2013 12:39:40 United States #
Willena

My research has been enhanced by you information, I'm thankful for the post

personal injury lawyers austin 1-1-2014 19:44:01 United States #
personal injury lawyers austin

content. I cherished your post! The place am i able to obtain out a lot more details about this? Is it alright to put element of the on my particular blog site if I submit a reference level to this webpage? Awesome article, many thanks, I will bookmark you now! Is it okay to location portion of this in my personal webpage if perhaps I submit a reference to this world-wide-web web-site? Indeed it's ok. This actually answered my dilemma, thanks! A insightful article there mate . Thanks for that ! I really like that website format . How do you help it become!? Its really interesting. Dude. This blog site is neat! How does one allow it to be search this great . It appears to me this world-wide-web internet site doesnt load up on a Motorola Droid. Are other people having precisely the same dilemma? I enjoy this site and dont wish to have to miss it any time Im absent from my computer. Have you ever considered including a couple of social bookmarking links to these blogs. In the pretty least for bebo. Have not imagined of that. Any Strategies on how to go about it? This truly answered my difficulty, thank you! In the event you dont thoughts, wherever do you host your weblog? I am looking for the superior internet host and your webpage appears to become particularly fast and up just about every one of the time I use hostgator Just to permit you know your web site seems to be a little bit unusual on Safari on my pc with Linux . It appears to be to me that this web site doesnt load on a Motorola Droid. Are other folks having the exact exact situation? I enjoy this webpage and dont wish to have to miss it when Im absent from my computer. Have you considered incorporating some differing opinions towards the article? I do think it will genuinely enhance viewers knowledge. This weblog web page is excellent. How did you enable it to be ? I purchased an book on world wide web designing and that i follows the step by step outline within. It is actually incredibly easy to utilize as I do not even know HTML. A appealing blog site article correct there mate . Thanks for submitting ! Just identified this page thru Yahoo, what a pleasant shock! Just identified this web site thru Google, what a pleasant shock! Desired to drop a remark and enable you realize your Feed is not functioning today. I tried introducing it to my Google reader account and got certainly absolutely nothing. Thanks for the observation but I have not but set up a feed for this site very handy stuff, total I picture this can be worthy of a bookmark, thanks If you could information me having a several hints & suggestions on how you manufactured your blog site web site glance this awesome, Id be appreciative. The appear for your internet site can be a tad off in Epiphany. Even So I like your site. I may well really need to set up a normal browser just to enjoy it. Have you ever assumed about incorporating some relevant links to the report? I do think it will really enhance viewers comprehending. Have you considered introducing some videos into the article? I think it will definitely enhance everyones knowing. I've not believed of that. Any Idea? Have you considered including some social bookmarking links to these websites. At the extremely least for bebo. Have not thought of that. Any Tips on it? This website web page has some definitely practical info on it! Cheers for helping me. I like your wordpress net template, exactly where did you get yourself a hold of it? Nice put up ! Cheers for, commenting on my blog webpage gentleman. Ill message you some time. I didnt realize that! As soon as I originally commented I clicked around the Notify me whenever new opinions are additional checkbox and now each time a comment is extra I receive 4 emails using the identical comment. When i start your Feed it appears to generally be being a great deal of garbage, will be the issue on my side? I enjoy your wordpress template, in which did you get yourself a hold of it? I downloaded it from the world wide web working with google lookup engine Hello just wished too provide you with a quick heads up. Thee terms within your

acne scars 13-1-2014 19:39:33 United States #
acne scars

I enjoy what you guys are usually up too. This kind of clever work and exposure! Keep up the amazing works guys I've included you guys to  blogroll.  <a href=http://acne.adsuse.com>best acne treatment</a>

Legginsy 16-1-2014 13:14:17 United States #
Legginsy

Outstanding post, you have pointed out some  superb  points , I  besides   conceive this s a very  great  website.

the original source 19-1-2014 10:35:07 United States #
the original source

I gotta bookmark  this  web site  it seems  extremely helpful  extremely helpful

pipe tobacco 23-1-2014 02:24:12 United States #
pipe tobacco

Hi there cos i so totally love your great site, i wuold be happy if i could write a long review about this wordpress blog on my would you say yes?

voyance gratuite immediate 24-1-2014 00:24:37 United States #
voyance gratuite immediate

logiciel horoscope gratuit de marseille<br/>Visiter notre superbe site web : <a href="http://paradiseradio.fr">voyance gratuite en direct</a>

voyance gratuite immediate 24-1-2014 05:53:18 United States #
voyance gratuite immediate

lire tarot par telephone toulouse<br/>Decouvrir notre nouveau post : <a href="http://paradiseradio.fr">voyance</a>;

Nidia 6-2-2014 15:26:55 United States #
Nidia

Will love to read more from you in the future, keep the good stuff coming.

voyance gratuite en ligne 8-2-2014 10:59:59 United States #
voyance gratuite en ligne

logiciel voyance par mail nice<br/>Decouvrir mon excellent site web : <a href="http://www.paradiseradio;fr">voyance par telephone gratuite</a>

voyance sans attendre 8-2-2014 11:10:27 United States #
voyance sans attendre

voyance par telephone amour ile de france<br/>Voir notre excellent site web : <a href="http://paradiseradio.fr">voyance gratuite en direct</a>

voyance immediate aujourd'hui 8-2-2014 12:57:36 United States #
voyance immediate aujourd&#39;hui

voyance par telephone  de marseille<br/>Voir mon superbe blog : <a href="http://www.paradiseradio;fr">voyance par telephone gratuite</a>

amoureuse voyance 9-2-2014 16:21:23 United States #
amoureuse voyance

voyance par telephone amour ile de france<br/>Visiter notre excellent blog : <a href="http://paradiseradio.fr">voyance</a>;

Cette voyance 10-2-2014 22:45:45 United States #
Cette voyance

voyance gratuite  de marseille<br/>Decouvrir ce nouveau blog : <a href="http://www.paradiseradio;fr">voyance en direct</a>

atomiseur ego-t 11-2-2014 11:47:01 United States #
atomiseur ego-t

Consulter notre excellent site : <a href="http://medicalcybrarian.com">Acheter cigarette ego-t</a><br/>

ekonomia 17-2-2014 12:39:51 United States #
ekonomia

Great information. Lucky me I came across your site by accident (stumbleupon). I have saved as a favorite for later!

seolite.eu 17-2-2014 16:39:11 United States #
seolite.eu

Wonderful points altogether, you simply gained a logo new reader. What would you recommend about your put up that you simply made some days in the past? Any positive?

brazil world cup 2002 squad 20-2-2014 21:47:59 United States #
brazil world cup 2002 squad

Portugal is at the moment rated four th in the FIFA standings, and with great cause. With stars these kinds of as Cristiano Ronaldo and Simao, you know they're in for a deep World Cup run for 2010. World Cup heritage has not been way too sort to the Portuguese with their best end coming in 1966 at three rd In 2006, Portugal finished 4 th, which may possibly have appeared limited of expectations. You know how a volcano lies dormant just before it erupts? That's the comparison I like to use for Portugal. The quarter finals will be held on 4 and five July. As properly as, semi finals will be held at eight and 9 July.|With their Spanish-dependent player Maradona, the men's soccer staff of Argentina failed to advance the semis soon after shedding (two-one) to eventual winner Italy and then with Brazil (3-one) in the 2nd spherical. Whilst the obvious-lower loser of the Spanish tournament was Brazil, curiously a soccer-loving nation that has by no means won a gold medal in men's football at the Olympic Summer Video games in contrast to Italy, Mexico, Uruguay, Argentina, and Nigeria. Nevertheless regardless of not profitable the FIFA World Cup, the 1982 Brazilian contingent was regarded as a single of the twentieth century's most fantastic teams. Socrates was Brazil's 1982 World Cup captain.But there ended up other interesting specifics.

free iphone application 22-2-2014 01:10:12 United States #
free iphone application

nice share,Thanks for the informations dont forget to drop by my link and apply to be able to win a free iphone 5c https://www.youtube.com/watch?v=Sp3wOejk52I

crystal 23-2-2014 06:42:16 United States #
crystal

The post was genuine helpful for my research, ideally I can discover more similar details

sites 24-2-2014 06:07:31 United States #
sites

obviously like your website but you have to test the spelling on quite a few of your posts. A number of them are rife with spelling problems and I to find it very bothersome to inform the truth then again I'll surely come back again.

Carlos Nosacka 25-2-2014 12:45:17 United States #
Carlos Nosacka

Hey There. I found your blog using msn. This is a really well written article. I'll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I'll definitely return.

Katelyn 1-3-2014 00:01:43 United States #
Katelyn

Thanks for your input, it help me immensely

Pamala 2-3-2014 02:53:47 United States #
Pamala

Nice work your blog is a great resource, especially for my research

Viviana 3-3-2014 20:46:23 United States #
Viviana

I like what you have done here, please keep providing stella information.

Lashell 5-3-2014 09:49:08 United States #
Lashell

My research has been enhanced by you information, I'm thankful for the post

engagement rings 6-3-2014 00:17:12 United States #
engagement rings

Hopefully I can come back and use more of your information, thanks for posting.

web-site 12-3-2014 01:22:51 United States #
web-site

I like this web site very much, Its a very nice office to read and find info. "We are all failures--at least, the best of us are." by James Barrie.

tootpaste 22-3-2014 13:06:08 United States #
tootpaste

I was just browsing, your post has actually been worth the search

visit page 21-4-2014 05:11:24 United States #
visit page

Regards  for helping out,  wonderful  information.

chatline 16-5-2014 04:06:00 United States #
chatline

Enjoyed reading  this, very good stuff, thanks . "Curiosity killed the cat, but for a while I was a suspect." by Steven Wright.

this astrology site 23-5-2014 15:20:19 United States #
this astrology site

Thankyou  for helping out,  excellent  info .

Facehook 13-6-2014 21:29:55 United States #
Facehook

Social marketing is a jungle, and you better learn to pimp your own profiles.

Become Popular 14-6-2014 22:53:12 United States #
Become Popular

Our mutual interest is obvious. Splendid!

snipping tool 15-6-2014 17:25:22 United States #
snipping tool

This was a fantastic read. Thank you for sharing it around! Nowadays the world wide web is full of poor content however, there is no doubt that you just spent long by editing the information. Again, appreciate your time as well as for your efforts!

The Art of Grabbing Attention 16-6-2014 13:38:18 United States #
The Art of Grabbing Attention

Hooray. A number of these methods are not on our list. Splendid.

Pantofi Barbatesti 18-6-2014 12:43:45 United States #
Pantofi Barbatesti

Simply because I initially wrote concerning MBT sneakers at <strong><a href="incaltaminte-mopiel.ro/.../category&path=18_46"; title="Pantofi Barbatesti">Pantofi Barbatesti</a></strong>, a ton includes improved more than the brand name, and the inventor and founder consists of still left the enterprise. Between true prospective buyers, testimonials of MBT footwear are pretty put together. If by yourself're thinking about a few of MBT sneakers, I strongly really encourage by yourself towards as well examine the opinions still left within answer toward the immediately after article content, and discuss towards your health practitioner just before putting on any design of shoe that is hence drastically option towards what on your own're made use of toward, or towards deal with any style of suffering or difficulty.

Kosiarki ceny 15-7-2014 08:37:36 United States #
Kosiarki ceny

It�s actually a nice and useful piece of information. I am glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

Dropbox Tips Tricks 29-4-2015 04:29:36 United States #
Dropbox Tips Tricks

After looking at a few of the blog posts on your web page, I seriously like your technique of writing a blog. I added it to my bookmark webpage list and will be checking back soon. Please check out my web site too and let me know how you feel.

bateria iphone 29-5-2015 09:43:38 United States #
bateria iphone

<p>999 A vital section of every woman wardrobe are her sneakers.<br /> <a href="www.kayakingplus.com/...ton-wallets-c-16.html"; rel="nofollow">cheap louis vuitton wallets</a></p>

Carroll B. Merriman 9-6-2015 17:48:34 United States #
Carroll B. Merriman

Czy mo�na upiec ciasto bez m�ki? Mo�na! A bez jajek? Te� mo�na! Ciasto powsta�o specjalnie dla mojej starszej c�ry, kt�ra ma uczulenie na bia�ko jaja kurzego i czeka na badanie w kierunku celiakii st�d te� od oko�o roku w naszym domu cz�stym go�ciem s� produkty bezglutenowe. Dodatkowo mamie zachcia�o si� zosta� wegank�, ale kocha smak czekolady, tato te� uwielbia s�odycze, m�odsza siostrzyczka te� ch�tnie li�nie, tak wi�c jest tou nas ciasto idealne dla ka�dego. No i przygotowuje si� je bez u�ycia miksera, bez brudzenia miliona gar�w i potrafi je przygotowa� (z ma�� pomoc�) moja 3-letnia c�rka. Tak�e serdecznie zach�cam do spr�bowania, bo naprawd� warto! Panie i Panowie: Czekoladowe, bezglutenowe vege-ciasto.Wi�cej informacji na moim blogu cotakpachnie.com.pl/.../

Milagros Muina 12-6-2015 22:30:21 United States #
Milagros Muina

This really is the worst write-up of all, I’ve study

pleasant 23-6-2015 19:58:21 United States #
pleasant

Amazing what you can come across

Kirk Tripoli 26-6-2015 10:59:14 United States #
Kirk Tripoli

Really good blog, thank you so much for your effort in writing the post.

Marco 10-7-2015 02:47:25 United States #
Marco

Hi everybody, I'm an Italian man decided to send a message which will describe my personality. Long ago there were transmitted values by GOD and JESUS, and at that time people felt it more consideration. Nowadays, the observance of these principles has started a process of decay. This consideration of the values is not only vital in personal relationships between men and women, but also towards present and future society, which learns and will learns our behaviour patterns. All this means that the more time passes, the less importance has the lives of others. A couple must have a very strong faith in order to address all the problems that life delivers, so it is necessary that each other are offset by a fidelity that is based precisely on the concepts above. IT'S ESSENTIALS THAT ALL PEOPLE DIFFUSE THE GOSPEL ALL OVER THE WORLD AS MANY PEOPLE WE CAN. My main characteristic is focused on being God-loving, for this reason i'm very reliable in relationships. Nobody is perfect, but this particular side makes safer a person who is interested to become a part of my life. I am willing to start something special that will lead to the development of a more solid bond. I'm available to relocate everywhere because for me geographical distance is purely a matter of jumping on a plane and next second virtual world became reality. Whatsapp: +447937463555; Viber: +393409282577; skype: testifi87

free mp3 download 16-7-2015 20:18:51 United States #
free mp3 download

Finding music that is both free and legal to download isn't always easy. ...

solar panels australia 23-7-2015 12:22:23 United States #
solar panels australia

I tried viewing your site on my iphone and the design does not seem to be correct. Might want to check it out on WAP as well as it seems most mobile phone layouts are not working with your web site.

unique bikinis 28-7-2015 23:36:24 United States #
unique bikinis

I've been absent for some time, but now I remember why I used to love this web site. Thank you, I'll try and check back more frequently. How frequently you update your site?

Agua Bendita Quetzal 29-7-2015 00:04:13 United States #
Agua Bendita Quetzal

bikini bottom 29-7-2015 03:23:00 United States #
bikini bottom

agua bendita veracruz 29-7-2015 03:27:25 United States #
agua bendita veracruz

agua bendita swim 29-7-2015 09:18:17 United States #
agua bendita swim

agua bendita limited edition 29-7-2015 11:10:27 United States #
agua bendita limited edition

I dugg some of you post as I thought  they were  extremely helpful  invaluable

Agua Bendita Acrobacia 6-8-2015 12:19:53 United States #
Agua Bendita Acrobacia