modify

Specifies the different data-modification settings for the remote data source. Can be one of:

1. A function expression that will handle the saving of all changes:

var dataSource = new shield.DataSource({
    remote: {
        modify: function (items, success, error) {
            $.ajax({
                type: "POST",
                url: "/MyApp/ApplyChanges.aspx",
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringofy(items)
            }).then(success, error);
        }
    }
});

The function accepts the following arguments:

  • items - A dictionary containing the unsaved changes as values for the "added", "edited" and "removed" keys. The value for each key will be a list of Model instances, each of which repsents an individual data item that was modified (created, updated or deleted). The actual data for an item can be accessed via its data property and it will be in the format defined by the DataSource's schema.
  • success - A function to call when the data is successfully saved.
  • error - A function to call when there's an error saving the data. An Error string or object must be provided as a first argument. See the error event for more information how this error can be handled in a uniform way.

2. An object literal containing the settings for the three data-modification operations - create, update and remove:

var dataSource = new shield.DataSource({
    remote: {
        modify: {
            create: function (items, success, error) {
                var newItem = items[0];

                $.ajax({
                    type: "POST",
                    url: "/book",
                    dataType: "json",
                    data: newItem.data,
                    complete: function (xhr) {
                        if (xhr.readyState == 4) {
                            if (xhr.status == 201) {
                                // update the id of the new item
                                // set the id from the location url
                                var loc = xhr.getResponseHeader("Location");
                                newItem.data.Id = +loc.replace(/^.*?\/([\d]+)$/, "$1");

                                // call the success callback
                                success();

                                return;
                            }
                        }

                        // call the error callback
                        error(xhr);
                    }
                });
            },
            update: function (items, success, error) {
                $.ajax({
                    type: "PUT",
                    url: "/book/" + items[0].data.Id,
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(items[0].data)
                }).then(success, error);
            },
            remove: function (items, success, error) {
                $.ajax({
                    type: "DELETE",
                    url: "/book/" + items[0].data.Id
                }).then(success, error);
            }
        }
    }
});