views:

53

answers:

2

When I am retrieving JSON entities via jQuery from the server and manipulating them client side, I have this desire to be able to see their interface via Visual Studio intellisense. So, ignoring whether this is a stupid idea, is this possible in any way?

So what I was thinking was being able to reference Entities.js, which would contain definitions for all entities, ie:

Class Person
   String Name
   String Address
   String Telephone
Enc Class

So then when writing client side javascript, these properties would be visible via intellisense.

I have a feeling this is not possible though? If not, could it perhaps be simulated via enums or something like that?

A: 

You can wrap the data into javascript classes, ie:

var someJson = { foo: 'foo', bar: 'bar' };

var MyClass = function(foo, bar) { this.foo = foo; this.bar = bar; }
MyClass.prototype = {
    foo: '',
    bar: ''
};

var someObj = new MyClass(someJson.foo, someJson.bar);

I don't know about Visual Studio, but IntelliJ IDEA and Aptana should be able to autocomplete this kind of structures quite nicely.

It also depends a lot on if the IDE is able to determine the type of a variable from somewhere. If you pass some object as a parameter to a function, the IDE may not be able to determine what the type is. This can usually be assisted by providing a type-hint in a JsDoc style comment or such.

These are really the kind of issues which require either JsDoc hints or some smart guessing by the IDE. In my experience, IntelliJ does the best job in this.

Jani Hartikainen
+1  A: 
CMS
Haven't confirmed this is true or not, but it seems reasonable and would acieve exactly what I want.
tbone