Quantcast
Channel: SmartClient Forums
Viewing all articles
Browse latest Browse all 4756

Get changed values in transformRequest after form.saveData()

$
0
0
SmartClient: SNAPSHOT_v9.1d_2013-09-08
Firefox 23.0.1
Client side-data-integration

New to SmartClient, so far looks very very nice, just a little trouble getting started.

I am using OrientDB as backend db/rest server and can't modify the format to use the RestDataSource to I am trying to convert the data sent back to the server to the correct format.

After calling form.saveData() I am using the transformRequest to manipulate the data before I post the update back to the server, however I do not know how to extract the changed values from the dsRequest.data, the data object contains all of the unmodified fields as well as the modified ones and I need to just send back the record id with just the modified values

I know the form object has getChangedValues() which works like I want it to but after calling saveData() and once I am in the transformRequest the dsRequest does not have a ChangedValues method.

Here is the data that I receive from my server
Code:

{ "result" : [ { "@class" : "Contact",
        "@fieldTypes" : "Addresses=e",
        "@rid" : "#9:0",
        "@type" : "d",
        "@version" : 15,
        "Addresses" : [ "#10:0" ],
        "ContactType" : "#11:0",
        "firstName" : "ttt",
        "lastName" : "green"
      } ] }

This data is bound to a grid that upon row selection sets the form values to current selected row

The update record posted to the server should look like
Code:

{ "transaction" : true,
    "operations" : [
          { "type" : "u",
            "record" : {
              "@rid" : "#9:0",
              "firstName" : "Joe",
              "lastName" : "Smith"
            }
          }
      ]
  }

I have no problems creating a new javascript object like the one above, but I don't know how to get the changed values from the dsRequest.data

Here is my test code
Code:

  RPCManager.allowCrossDomainCalls=true
    var dbcommand = 'select from #9:0'
    var dbcommandUpdate =  'update #9:0 set firstName="ttt"'
    isc.DataSource.create({
            ID: "contactsDS",
            dataFormat: "json",
            recordXPath:"/result",
            fields: [
                {name: "@rid", title: "rid",primaryKey:"true",hidden:true},
                {name: "firstName", title: "First Name"},
                {name: "lastName", title: "Last Name"}
            ],
        transformRequest : function (dsRequest) {
            debugger;
            var mydata = dsRequest.data
            if (dsRequest.operationType == "update") {
                //format data
            }
            else
            if (dsRequest.operationType == "add"){
              //format data
            }
            dsRequest.contentType = "application/json";
            return JSON.stringify(mydata)
        },
        operationBindings:[
            {operationType:"fetch",
                dataURL:'http://localhost:2480/command/RestTester/sql/' + encodeURIComponent(dbcommand).replace(/'/g, "%27").replace(/"/g, "%22"),
                requestProperties:{httpHeaders:{"Authorization": "Basic YWRtaW46YWRtaW4="}}
            },
            {operationType:"add",
                dataURL:'http://localhost:2480/command/RestTester/sql/' + encodeURIComponent(dbcommand).replace(/'/g, "%27").replace(/"/g, "%22")
            },
            {operationType:"update",
                dataURL:'http://localhost:2480/batch/RestTester',
                dataProtocol:"postMessage"
            },
            {operationType:"remove",
                dataURL:'http://localhost:2480/command/RestTester/sql/' + encodeURIComponent(dbcommand).replace(/'/g, "%27").replace(/"/g, "%22")
            }
        ]
        })

  // recordClick: "contactsForm.editSelectedData(contactsList)",
    isc.ListGrid.create({ ID: "contactsList",
        left: 50, top: 50, width: 500,
        recordClick:function (viewer, record, rowNum, field, fieldNum, value, rawValue) {
            contactsForm.clearErrors();
            contactsForm.editSelectedData(contactsList);
            saveButton.enable();
        },
        dataSource: "contactsDS",
        autoFetchData: true });
    isc.DynamicForm.create({ ID: "contactsForm", left: 50, top: 200, width: 300, dataSource: "contactsDS" });

 isc.IButton.create({
    ID: "saveButton",
      disabled:true,
    left:175, top:280, width:150,
    form:contactsForm,
        title:"Save",
        click:"contactsForm.saveData()"
    })
  function test(){
      debugger;
    var a = contactsForm.getChangedValues()
      var b = contactsForm.getOldValues()
      var c = contactsForm.valuesHaveChanged()
    //  var d = contactsForm.value
  }

Also is this the correct approach for connecting to a Rest API versus overriding the built in RestDataSource, I think the docs said this was the correct approach, but just want to confirm. After I get this working then I should be able to extend the DataSource class to include the necessary modifications to communicate to my server, any examples of other users making a custom js DataSource class to a custom rest server?

Thanks for any help,
Dan

Viewing all articles
Browse latest Browse all 4756

Trending Articles