Announcement

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

    updateData error

    Hi, i prove the code (provided in a previous message) but i find the following problem:

    using saveEdits in the 'update' button with autoSaveEdit:false (or automatic update when user change to another row in listgrid with autoSaveEdit:true). in both cases the web service is called only passing it (like parameters) the primary key field and the changed field, producing and error because the webservice function expects all the parameters.the question is: ¿ how can i pass all the parameters to the webservice when an update occurs?.
    Reply With Quote

    #2
    Hi Alex,

    When a DataBound component (ListGrid, DynamicForm, ...) initiates a save, it sets dsRequest.oldValues to the original values from when editing began. You can provide an implementation of dataSource.transformRequest() that combines the changes with the original values, like so:

    Code:
    transformRequest : function (dsRequest) {
        return isc.addProperties({}, dsRequest.oldValues, dsRequest.data);
    }
    Last edited by Isomorphic; 10 Sep 2008, 17:16.

    Comment


      #3
      Hi,

      This is helpful to me also. But...

      1. There are 2 typos. tranform should be transform, and the curly brace after oldValues does not belong.

      2. When I tried this, my ListGrid no longer filters properly...it shows all records. Despite my having used an if condition for the update operation only.

      Code:
      	transformRequest : function (dsRequest) {
      		if (dsRequest.operationType == "update") {
      			return isc.addProperties({}, dsRequest.data, dsRequest.oldValues);
      		}
      	}

      3. How do I read the old values in my back-end PHP script? What naming convention is used? old_[variable name] ?

      Thanks,
      Mike

      Comment


        #4
        1. thanks, the code snippet has been corrected.

        2. doesn't make much sense, suggest you look into this again

        3. oldValues are not passed by default, but you could pass them with a prefix in the manner you describe

        Comment


          #5
          3. I thought by doing the isc.addProperties, that's what triggers the passing of the old values...

          Mike

          Comment


            #6
            3. Yes it does, however in this case the old values have the same field names as the original values, so there's no way for the backend to distinguish between them.

            Using a for..in loop to add the oldValues with a prefix like "old_" would allow the backend to distinguish between the original and new values, similar to the Java server, where they are available as separate Maps.

            Comment


              #7
              I assume I would write the for loop in the transformRequest function.

              Are there any samples of this?

              My guess is that I would have to change dsRequest.oldValues somehow, but I'm not sure of the syntax for doing that.

              Thanks,
              Mike

              Comment


                #8
                It's just JavaScript, something like:

                Code:
                    var data = isc.addProperties({}, dsRequest.data);
                    for (var fieldName in dsRequest.oldValues) {
                         data["old_" + fieldName] = dsRequest.oldValues[fieldName];
                    }
                    return data;

                Comment


                  #9
                  For me, I am editing the whole row and trying to send a combination of the changed values and also the old values for the ones that were not changed. I am using an Oracle pl/sql backend and it's easier for me to do my SQL update that way, rather than having just the new values sent.

                  When I tried the code mentioned above
                  Code:
                  transformRequest : function (dsRequest) {
                      return isc.addProperties({}, dsRequest.data, dsRequest.oldValues);
                  }
                  it just returned the old values.

                  I am now using:
                  Code:
                  transformRequest : function (dsRequest) {
                    var data = isc.addProperties({}, dsRequest.data);
                      for (var fieldName in dsRequest.oldValues) {
                        if (data[fieldName] === undefined) {
                          data[fieldName] = dsRequest.oldValues[fieldName];
                        }
                        else if (data[fieldName] === null) {
                          data[fieldName] = '';
                        }
                      }
                    return data;
                  }
                  Does that look like it will work without issues? It seems to work OK, but I just want to double check it because I don't want to risk losing any data. Is there a better way to do that?

                  Thanks.
                  Last edited by ProbablySlackin; 13 May 2008, 11:35.

                  Comment


                    #10
                    Remember, the field names in oldValues are the same as those in data. From your code, it looks like you would be replacing the current value with the old value. To pass the old values, you should append something to the field name, like "old_" + fieldName, as was mentioned in the previous posts.

                    Comment


                      #11
                      My intentions are to replace values.

                      My current grid has appx 25 columns, so I have to declare 25 incoming variables in my database stored procedure. If I append old_ to the variable names, I would have to declare another additional 25 incoming variables, then provide logic in the stored procedure to compare the values before updating my table.

                      With the transformRequest method I posted above I just have to declare one stored procedure incoming variable for each column in my grid, then do a simple SQL update in my stored procedure without any further logic necessary there.

                      Once again, I'm looking for validation from you, as I have not done listgrid edits before and want to make extra sure I am going in a direction where I am not going to lose any data. Thanks.

                      Comment


                        #12
                        That should work fine from what I can see. No data should be lost, but the only really sure way to know is to test a few scenarios and monitor what you're recieving on the back end to make sure everything's there.
                        So the point of your code then from my understanding is to make sure there are no empty values passed back to the server, correct?

                        Comment


                          #13
                          That is correct, I want to make sure I am sending a value (possibly null) for each column every time. I have tested it and it does seem to be working fine. The only issue I was hitting originally was when there was a non null value for the old value that gets changed to a null value, but I think I resolved that by using the === to differentiate between nulls and undefined's.

                          Thank you, this functionality of editing directly in the listgrid is really slick and is going to make the whole editing process easier and quicker for myself and the end users. Great stuff.

                          Comment


                            #14
                            I used a similar back-end technique to get around this.

                            I set up a series of if/then statements when constructing the SQL UPDATE statement, checking to see if a value was passed or not. If so I add it to the SQL statement.

                            Granted, this has a couple limitations. The back-end update logic is cluttered, so you'd only want to do it for a small number of fields. Also, blank values can't be updated, since I'm using blanks to determine if a field has been changed in the ListGrid.

                            I will try to implement ProbablySlackin's technique instead. My technique, while flawed, got me by until I could learn this system better.

                            Mike

                            Comment


                              #15
                              I finally had time to implement ProbablySlackin's solution, and it works beautifully. He (or she) is definitely NOT slackin.

                              Kudos for using === (the identity operator). Had to look that up.

                              I would think this would be a common request, at least for people using REST datasources in a serverless environment (with PHP or ASP, where you need to build a SQL UPDATE statement for all datagrid values, not just the changed values.)

                              It makes the datasource code a little more complicated, but allows the back-end code to be simpler.

                              Would this be a valid "wishlist" request as an option to toggle in the datasource, like "sendAllValues:true" ? (Rather than having to add a transformRequest to each datasource).

                              Below is the code I used, in case anyone needs it.

                              Code:
                              
                              // item file
                              var itemFields = [
                              	{name:"item_id", primaryKey:true},
                              	{name:"item_descrip"},
                              	{name:"item_cat_id"},
                              	{name:"item_qb_fullname"},
                              	{name:"item_unit1"},
                              	{name:"item_unit2"}
                              ];
                              
                              isc.RestDataSource.create({
                                  ID: "itemRestDS",
                                  dataFormat:"json",
                                  fetchDataURL:"/json/itemRestDS.php?request_type=fetch",
                                  addDataURL:"/json/itemRestDS.php?request_type=add",
                                  updateDataURL:"/json/itemRestDS.php?request_type=update",
                                  removeDataURL:"/json/itemRestDS.php?request_type=remove",
                                  fields: itemFields,
                                  transformRequest: function (dsRequest) {
                              		if (dsRequest.operationType == "update") {
                              			
                              			// if we are updating, we want to send all values
                              			// Not just the changed values
                              			// Otherwise we need to check each value
                              			// in the back-end PHP
                              			
                              			var data = isc.addProperties({}, dsRequest.data);
                              
                              			for (var fieldName in dsRequest.oldValues) {
                              				if (data[fieldName] === undefined) {
                              					data[fieldName] = dsRequest.oldValues[fieldName];
                              				}
                              				else if (data[fieldName] === null) {
                              					data[fieldName] = '';
                              				}
                              			}
                              
                              			return data;
                              		}
                              	}
                              });
                              Last edited by msatkevich; 5 Sep 2008, 17:15.

                              Comment

                              Working...
                              X