CSLA with JQuery

CSLA with JQuery

Old forum URL: forums.lhotka.net/forums/t/10378.aspx


fark posted on Tuesday, May 17, 2011

 I have an employee CSLA object that is a root with a number of child collections.  One of the child collections is a List of phone numbers.  I would like to have a button that calls the addNew for the PhoneList.  To do this with JQuery do I have to create some code to serialize my object?  I didn't really want to write a lot of this because it will be a lot to maintain down the road and this object will have a lot of fields and child objects.

I initially tried this:

        var urlAdd = '@Url.Action("AddPhone")';
        $.get(urlAdd, function (data) {
           $('#Phone tbody>tr:last').after(data);
            window.alert(data);
        });

        public ActionResult AddPhone()
        {
            EmploymentApplicationViewModel viewmodel = new EmploymentApplicationViewModel();
            Phone phone = viewmodel.ModelObject.PhoneList.AddNew();

            phone.NameCH = "TestNewAdd";
            phone.AddressCH = "TestnewAdd";
            PartialViewResult pvr = PartialView("_AddPhone", employmentapplication);
            return pvr;

        }

The code above did call the AddPhone method on my controller just fine and added a new phone to the CSLA object just fine.  It would return back the new phone to my table and inserted the new row just fine.  Then I realized the object I was working with in my controller was blank and realized I needed to pass in my object.  So I started looking at this:

   function addPhoneNumber() {

        var employee = {
        IdEmployee: idemployee,
        NameEmployee: name,
        iddepartament: dept

        PhoneList  = ?
    };

        $.ajax({
            type: "POST",
            url: urlAdd,
            data: JSON.stringify(employee),
            datatype: "JSON",
            contentType: "application/json; charset=utf-8",
            success: function (returndata) {
                if (returndata.ok) {
                    $('#Phone tbody>tr:last').after(data);
                    window.alert(' saved ');
                }
                else {
                    window.alert(' error : ' + returndata.message);
                }

            }
        });
    }

RockfordLhotka replied on Tuesday, May 17, 2011

From a software architecture perspective, you should think about the problem space as consisting of two seperate applications.

On the client you have a smart-client application written in js. It is communicating with a service by sending and recieving messages.

On the server you have an application with a service interface (XML or JSON probably). Consuming applications interact with this service interface.

So yes, your client application will need to "serialize" its objects to create the message that is sent to the service interface of the application running on the server. That's not really serialization in a strict sense though, because it is creation of a message object, not a data representation of the original object.

Similarly, the service application will need to create and consume message objects from its service interface. I discuss this in Chapter 21 of the Expert 2008 Business Objects book.

Copyright (c) Marimer LLC