1

Is it possible for backbone to interface with asp.net soap webservice methods for saving and retrieving the data? because i got this error from the webmethod but actually the POST contains the parameters.

Server Side

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static Dummy SaveDummy(Dummy myDummy)
        {
            Dummy dumdum = myDummy;
            HttpContext.Current.Session["data"] = dumdum;
            return myDummy;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public static Dummy FetchDummy()
        {
            return (Dummy)HttpContext.Current.Session["data"];
        }

        public class Dummy
        {
            private string _name;
            private string _script;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public string Script
            {
                get
                {
                    return _script;
                }
                set { _script = value; }
            }
        }

Backbone Model

  window["model"] = Backbone.Model.extend({
        initialize: function () {
            console.log("CREATED");
        },
        defaults:{
            name:"Please enter your name",
            script:"Hello World"
        },
        urlRoot: "index.aspx/SaveDummy",
        validate: function (attrs) {

        }
    });

Application

$("document").ready(function () {

    var myModel = new model({
        name: "Stack Overflow",
        script: "alert('Hi SO')"
    });

    var myView = new view({
        model: myModel,
        el: $("#placeholder")
    });

    console.log("SAVING");
    myModel.save();

    console.log("FETCHING");
    myModel.fetch();

POST

{"name":"Stack Overflow","script":"alert('Hi SO')"}

Message

Invalid web service call, missing value for parameter: 'myDummy'.

Note

I did look into other posts with similar problem, which were solved by doing something like {myDummy={"name":"Stack Overflow","script":"alert('Hi SO')"}} . How could this be generated using Backbone?

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

1 Answers1

2

All of the server-side synchronization in Backbone is handled through Backbone.Sync which is designed for two things:

  • REST apis that work with JSON (not SOAP/XML)
  • Extensibility

So you will need to override the Backbone.Sync behavior to talk to your backend. It appears to be relatively straight-forward. Some guidance can be found in these links:

Community
  • 1
  • 1
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • thanks for the answer again. Are you in anyway associated with Backbone? you are the one who has been answering all my queries lately with javascript and backbone. Do you have a blog that i can follow :) – Deeptechtons Oct 06 '11 at 11:27
  • :) No, I just use SO to hone my skills. Backbone/CoffeeScript are my new technologies I am trying to hone right now... I do have a blog (in my bio) but I haven't written anything about Backbone... I haven't really had much to say about it yet. – Brian Genisio Oct 06 '11 at 11:30