create

Specifies the remote settings for creating data. Can be one of:

1. A string containing the URL to send the data to:

var dataSource = new shield.DataSource({
    remote: {
        modify {
            create: "/create.php"
        }
    }
});

This will cause the DataSource to send the new data to the specified URL using a POST request. The data being sent is a dictionary and the created item(s) can be found in the list under the "added" key in that dictionary:

{
    "added": [{...}, {...}, ...]
}

Note: As the DataSource contains multiple/batch editing operations, the data dictionary can also contain lists with items being updated and deleted under the "edited" and "removed" keys.

2. An object literal containing various specific settings:

var dataSource = new shield.DataSource({
    remote: {
        modify {
            create: {
                data: function(items) {
                    return JSON.stringify(items[0].data);
                },
                url: "/book", 
                type: "PUT",
                contentType: "application/json"
            }
        }
    }
});

The data setting can optionally be used used to provide a transformation function that the DataSource can use before sending the data. The rest of the parameters are jQuery.ajax() specific parameters and can be used to configure the XMLHttpRequest object used for sending the data to the server, like the request type, headers, and other properties.

3. A function expression that sends the created items to a remote endpoint:

var dataSource = new shield.DataSource({
    remote: {
        modify {
            create: function(items, success, error) {
                $.ajax({
                    type: "POST",
                    url: "/book",
                    contentType: "application/json",
                    data: JSON.stringify({
                        name: items[0].data.name,
                        author: items[0].data.author,
                        price: items[0].data.price
                    })
                }).then(success, error);
            }
        }
    }
});

The function accepts the following arguments:

Argument Name Description
items A list of Model instances, each of which repsents an individual data item. The actual data can be accessed via its data property and it will be in the format defined by the DataSource's schema.
succcess (Function) A function to call when the data is successfully saved.
error (Function) 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.