Javascript Intellisense, Pretending to Compile JS

So Visual Studio 2010 is reporting everything has the same methods, the methods of the JavaScript Object.

Getting Intellisense to work
1) Maybe the Telerik ScriptManager stomped it. The ScriptManager has to be an Asp:ScriptManager and nothing else. (As of VS2010)
2) Maybe VS wants a Ctrl-Shift-J (manually force a JS intellisense update)
3) Maybe there is a “compile” error.
– Check the Error List tab and look at the yellow warnings.
– Check the General output window, especially after doing a Ctrl-Shift-J
– Try to reformat the code. If it doesn’t reformat, Visual Studio probably can’t “compile” and doesn’t know what to do
– Look for green squiggles
4) Maybe the JS wants to be in its own file. I’ve seen broken intellisense start working after the code was moved from an aspx to a .js file
5) Maybe the the annotations (the fake reference at the top of a JS page) are in the wrong order. For example, if you are working with Telerik, the Ajax reference should be first, Telerik stuff next, your own code later and it should be in order of dependency.
6) Maybe you haven’t added enough annotations (especially the fake “references”, but also summary, param, returns annotations)
7) Maybe you used a golden nugget i.e. <%= Foo() %> and put it in a JS block (on an ascx or aspx page of course.) The VS Javascript parser see this as JS and tries to treat it as malformed JS. When you can cheaply quote it, quote it.
- e.g. var foo =”<%= Foo() %>“; // Just a string.
- e.g. var bar =parseInt(“<%= Bar() %>“,10); //Convert to int
- e.g. var bar =”<%= TrueOrFalse().ToLower() %>“===”true”; //Convert to bool
- but maybe/maybe not e.g. eval(“<%= GenerateJS() %>“); //This isn’t a nice solution because you are doing an unnecessary, expensive, logic changing eval just to keep intellisense from breaking.
8) Watch this space, I still haven’t gotten page method intellisense to show up reliably.
9) ScriptMode appears to affect intellisense. ScriptMode=”DEBUG” has better intellisense, but literally 1000x worse performance for browser execution, especially on IE.

And an mistake to avoid especially for ASP.NET developers
<%= Foo() %> syntax does not work in a .js file. .js files are static and not processed by the ASP.NET templating engine.
JS values written to the screen are initial values. Once they are written, they might as well be static. The JS is code generated on the server, but executed on the client.
var now = ‘<%= DateTime.Now.ToString() %>‘ ; // This isn’t going to change.
If you call page methods, they return immediately, the call back happens a few seconds later.
If you page methods blow up, Global Asax will not get an error event, so you have to use try/catch in your Page Method.
If a page method blows up, it may start erroneously reporting “Authentication Failed” errors. I think this is some version of a WCF style logic, where a client can go into a “faulted” state and just refuse to behave there-after. Still a theory.
On a single page application (SPA), var === Session. In a multi-page ASP.NET application, you constantly store state in Session because values don’t live past the life of a page. In a single page application, your user doesn’t change pages. So a page variable is Session. It never times out.
All parameters of your page methods are user input. In server side programming, you might grab a value from the database, store it in Session and use it later to save a record. In the SPA scenario, that value is handed over to the user and they can change it before it is submitted back to the page method. The level of difficulty is not especially high. So as values pass from server to JS page and back, they will have to be re-validated. Even if you try to keep the values on the server alone, eventually the user will be given a choice of values, and on the page method, you’d want to validate that these values were on the list.

Javascript Books I’ve read

I am ASP.NET/C# developer and all of a sudden I needed to write a mostly client-side line of business web-app. So I started reading. This has taken up much of my train time for the last half year.

Books I recommend.
Professional JavaScript for Web Developers, Zakas. Required reading, beginner to intermediate. Some basics are covered, but the book is also encyclopedic, so some content is stuff that an intermediate or advanced dev would care about, such as the less common APIs.

Javascript Patterns. Beginner to intermediate. Great book. I’ve found that a lot of concept don’t sink in from just one reading from just one author. This book is a good complement to Zakas.

Eloquent Javascript- Fantastic book for intermediate dev. Some of the later chapters dragged because I couldn’t care about the game. But sometimes a good sample app is a good thing. Five of Five stars. (Also, this book had the best intro to functional programming without feeling like a math textbook.

Effective Javascript. Intermediate to advanced. Covers all the less common challenges you run into writing JavaScript. Required reading.

High Performance JavasScript AND Even Faster Websites. Intermediate. Team books– I now confuse them in my memory, chapters of various quality, some advice is not so general (well what if you aren’t doing many pictures)

Functional Javascript. Mostly advanced, some intermediate. I liked 1/3 of this– that is about 1/3 of each chapter. The other 2/3 were written for someone much smarter than me, possibly some one so smart that if they were that smart, they wouldn’t need to read a book about functional programming, they’d just do it spontaneously. Sample code was overly compressed, often read like algebra proofs with too many “easy” and “obvious” steps skipped over.

Effective REST Services with .NET. Has some Javascript, but the focus is on RESTy things.

The Art of Readable code. Not JS specific, but it was a good book.

Books I don’t recommend
Javascript & jQuery the missing manual. Beginner. Like you don’t know how to program in any language yet beginner. I bought this by mistake. But I would use this to teach my kids to program.

JavaScript Web Applications, MacCaw. Mostly advanced, some intermediate. This is about Spine actually. I wish it had that in the title. This was written for someone smart enough to write their own MVC framework.

(Where is Definitive Guide and the Good Parts?)
I use jslint, which imho, is a substitute for actually reading the Good Parts. Definitive Guide, last time I read it was like reading machine generated javadoc. I don’t know if that is still fair, but it kept me from buying an updated version.

Pluralsight Videos I’ve watched
I never know what sort of input will make my brain understand something in IT. Will it be hands on development? A book? A screencast? An audio podcast? So I try them all. If I sound lukewarm about some of these (the Knockout and Underscore videos), its because these libraries aren’t going to sink in for me until I work with them. But if I didn’t watch the video, I probably would have a painful time of getting started in the first place. And I find watching training videos to be work. This isn’t Game of Thrones.

JsRender by John Papa. Watch it. After it, you will understand what clientside templating is about. I don’t care if JsRender wins market or mindshare, it sounds like the templating technologies are all similar, so learn one, and you have a vague idea of how they all work.

Structuring JavaScript Code. Not bad. The focus is on “class”-ical programming. BUT, these are the easiest to grok ways to make your code modular.

Underscore Fundamentals. I wanted to benefit more from this than I did. I don’t know if there is a way to absorb the underscore library except through constant attempts to use it. I recommend taking breaks (watch it over a period of days) with coding time in between, to increase the odds of all those methods sinking in.

Knockout fundamentals. I think I get why these client side databinding engines exist now.

Podcasts
I listen to Hanselminutes, Dot net Rocks and Herding Code. I listen to these to learn that a technology exists, and who and what it is for. I learned about unit testing, build servers, and the like from podcasts. Despite listening to lots of JavaScript shows, none of them prepared me as well as the books and screen casts did.