Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Get changed values in transformRequest after form.saveData()

    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

    #2
    You can set dataSource.sparseUpdates to cause only changed values to be sent to the server. You can alternatively compute the list of changed fields yourself by comparing dsRequest.data and dsRequest.oldValues in transformResponse.

    Bigger picture, in most cases it is a bad idea to try to adapt to an existing REST API rather than simply implementing the built-in RestDataSource protocol. This FAQ comprehensively explains why.

    Note that @rid is not a valid field name (must be a valid JavaScript identifier).

    Comment


      #3
      sparseUpdates was what I was looking for I just missed it when browsing the api, thanks.

      I understand what you are saying about using the RestDataSourece, and I will look to see what it might take to adapt OrientDb's rest request/response.

      Comment

      Working...
      X