Modify objects returned by WebService

Last post 01-28-2008 2:04 PM by RTernier. 10 replies.

Sort Posts:

  • Modify objects returned by WebService

    01-25-2008, 1:27 PM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

    When an object is returned from an WebService that is called by javaScript, is it possible to either:

    a) modify that object and add additional functions, and properties to it

    or

    b) have .NET use an object you've already designed in JavaScript as the return type?

     Any help would be appreciated.
     

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


  • Re: Modify objects returned by WebService

    01-25-2008, 1:40 PM

    Just create a server-side class that has whatever methods and functions you want and make that your return type.  When it get's serialized and returned, the javascript object will automatically match it.

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: Modify objects returned by WebService

    01-25-2008, 3:34 PM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

    that's what I'm currently doing - I have my custom obejct that's being returned... a large object... with a property that has an array that can have over 100,000 items.... But what about functions and methods?

     if I want to do:

     
    myResult.getHTMLData();

    I need to have a getHTMLData() function on my object prototype don't I?  Or... ?

     

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


  • Re: Modify objects returned by WebService

    01-25-2008, 4:04 PM
    • Star
      13,533 point Star
    • gt1329a
    • Member since 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 2,231
    • TrustedFriends-MVPs

    You can add that on the client side, through your object's prototype.  For example, if your custom class is called MyClass, then:

    MyClass.prototype.getHTMLData = function() {
    return '<div>' + this.property1 + this.property2 + '</div>';
    }
      
    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Using jQuery validation with ASP.NET WebForms
  • Re: Modify objects returned by WebService

    01-25-2008, 5:14 PM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

     Exactly what I was looking for. I wasn't sure if the objecft returned was the same name of the class you creatd. Thanks.

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


  • Re: Modify objects returned by WebService

    01-25-2008, 5:57 PM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

    It didn't work as expected:

     

    My C# Webservice Function:

     
    public Entities.GPSData[] getMarkers()
    {...}

    -----

    GPSData is inside the namespace:

    BicNet.Entities

    so  BicNet.Entities.GPSData

    ----

    I tried doing the following:

    BicNet.Entities.GPSData.prototype.test = function()
    {
        return 'test';
    }

     

    which didn't work. So I tried:

    GPSData.prototype.test = function()
    {
        return 'test';
    }

     again didn't work.

    Any thoughts?
     

     

     

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


  • Re: Modify objects returned by WebService

    01-25-2008, 6:23 PM
    • Star
      13,533 point Star
    • gt1329a
    • Member since 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 2,231
    • TrustedFriends-MVPs
  • Re: Modify objects returned by WebService

    01-25-2008, 7:39 PM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

     I've looked at it, but it's still not working the way the example tells me it should.

    Type.registerNamespace('BicNet.Entities');

    BicNet.Entities.GPSData = function()
    {
        this.test = "testing";
    }

     now BicNet.Entities.GPSData (the object i just scripted above) has a property called test,  but this property cannot be accesed by the objects returned from my webservice (which are of type: BicNet.Entities.GPSData    C#).

    When looking in FireBug the result comming back has a property:     _type=BicNet.Entities.GPSData

     

    Unfortunately  in JS you typeof(object) will return: object, string etc. not the actual type.  I wonder if the object returned from my WS is one I can't get ahold of... 

     
    Thoughts?
     

     
     

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


  • Re: Modify objects returned by WebService

    01-28-2008, 11:49 AM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

    If I do this:
    BicNet.Entities.GPSData.prototype.test = function()
    {
        return 'test';
    }

     I can then do this:

     var d = new BicNet.Entities.GPSData();

    which gives me an object of BicNet.Entities.GPSData however, any object returned in the resultset (which is of type BicNet.Entities.GPSData()) does not have this function on it.  I'm not registering any new namepsace for this. I'm using the same namespace already created by ASP.NET which resides in my webservice.asmx/js   section.

     How can I get this function which I have above on those objects returned?
     

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


  • Re: Modify objects returned by WebService

    01-28-2008, 1:26 PM
    Answer
    • Star
      13,533 point Star
    • gt1329a
    • Member since 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 2,231
    • TrustedFriends-MVPs

    I got your email and took a look at the JS proxy generated by your service.  I also did some testing with a similar service I have set up that returns a collection of custom objects and is called through the ScriptManager via that same type of JS proxy.

    I ran into the same issues you're describing.  After rooting around in the MicrosoftAjax.js code, it looks like the problem is that the JavaScript deserialize function just evals the JSON into an array and doesn't actually instantiate a JavaScript object for each item.  So, the JavaScript OO stuff doesn't behave as expected on those.

    For now, you could always set up a helper function on the client side that implicitly accepts the data structure returned by your service and returns the HTML you want.  It wouldn't be as elegant a solution as what you're after, but might be the only reasonable way to do it within the current confines of the framework.

     

    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Using jQuery validation with ASP.NET WebForms
  • Re: Modify objects returned by WebService

    01-28-2008, 2:04 PM
    • Contributor
      4,490 point Contributor
    • RTernier
    • Member since 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,069

    That's one thing I was thinking. It's strange though that I can create a class of that type, but yea, it would be nice if they used an actual object that can be recreated not an array of objects created on the fly. 

    I was stepping into all the script created by MS Ajax, and sort of came to the same conclusion.

     

    I guess i'll just write my custom classes and when I get the result set, i'll move the results into my own objects... won't be that much work :)

     

    Thanks again. 

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog


Page 1 of 1 (11 items)