Question

There's a little problem with Visual Studio 2010 and Javascript Intellisense.

I've implemented a class with some "Properties" and want to implement a "static" function, which returns a new instance of the class after an ajax-request which returns a Json-Object.

Like so:

/// <reference path="jQuery/jquery-1.4.1-vsdoc.js" />
MyClass = function (options) {
    /// <summary>MyClass Description</summary>
    /// <param name="options" type="Array">Foo1 (string), Foo2(string)</param>
    /// <field name="Foo1" type="String">Foo1 Description</field>
    /// <field name="Foo2" type="String">Foo2 Description</field>

    // [...] Some Code

    // Properties
    this.Foo1 = options.Foo1;
    this.Foo2 = options.Foo2;
}

And the function:

intellisense not working:

MyClass.MyFunction = function () { 
    /// <summary>MyFunction Description</summary>
    /// <returns type="MyClass">MyClass</returns>

    $.ajax({
        type: 'GET',
        url: '/Foo/Bar',
        dataType: 'json',
        success: function (result) {
            return new MyClass(result);
        }
    });
}

intellisense working:

MyClass.MyFunction = function () { 
    /// <summary>MyFunction Description</summary>
    /// <returns type="MyClass">MyClass</returns>

    var foo = new MyClass({'foo1': 'a', 'foo2': 'b'});
    $.ajax({
        type: 'GET',
        url: '/Foo/Bar',
        dataType: 'json',
        success: function (result) {
            foo = new MyClass(result);
            return foo;
        }
    });

    return foo;
}

When I call the function from another function, like:

$(document).ready(function() {
    var foobar = MyClass.MyFunction(); // returns Object of type "MyClass"
    alert(foobar.Foo1); // At this Point, the intellisense doesn't work correct
});

my intellisense isn't working anymore (or works with the double-return), because the return of MyFunction is within the ajax-request. If I place the return at the end of the function, the intellisense is working again. But in this case I have two returns. The first from the function and the second from the ajax-success.

It seems that the <returns...></returns> only works when the return is at the end of the function. That's bad, because I just need one return when the ajax-request is completed.

I don't know how to deal with this problem. Hope you can help me to fix this :)

Was it helpful?

Solution

The return from inside the "success" callback is not going to work anyway. It'll return from that function, but nothing is going to pay attention to the return value, and in particular the return value from the "success" function will not be the return value from "MyFunction".

If what you want is for "MyFunction" to fetch a value and allow some code to work on it, then that code will have to be passed in to "MyFunction" and called in the "success" function. Thus, instead of:

var thing = MyClass.MyFunction();
/*
  do stuff with "thing"
*/

you'd change "MyFunction" around so that you could do this:

MyClass.MyFunction(function(thing) {
  /*
    do stuff with "thing"
  */
});

The function would look something like this:

MyClass.MyFunction = function (doStuff) { 
  /// <summary>MyFunction Description</summary>

  $.ajax({
    type: 'GET',
    url: '/Foo/Bar',
    dataType: 'json',
    success: function (result) {
        doStuff(new MyClass(result));
    }
  });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top