Learn how Scrivito CMS can help you deliver amazing digital experiences
See Scrivito CMS in action

Creating and Deleting CMS Objects

Creating and Deleting CMS Objects

Creating CMS objects: scrivito.create_obj

scrivito.create_obj(attributes) --> Promise()

Use this function to create a new CMS object in the current working copy. The attributes of the new CMS object can be passed in as an object whose keys become the attribute names. The values of the keys become the attribute values.

The format of the attribute values is identical to the format used in the Ruby API with a couple of additional possibilities:

For binary attributes, you may pass in a JavaScript Blob or File object. The latter inherits from the former. The object is automatically uploaded to the CMS.

  • For attributes of the date type, you may pass in a JavaScript Date object. It is automatically converted to the date format of the CMS.

The function returns a jQuery Promise. Once the Obj has been successfully created, the promise is fulfilled, and its success handler is called with an object, {id: "new-id"}. new-id is the ID of the Obj that has been created. An example:

var random_obj_path = "test" + Math.floor(Math.random() * 1000); scrivito.create_obj({title: "Test Obj", _obj_class: "Document", _path: random_obj_path}).then( function(data) { alert("Obj with ID " + data.id + " has been created"); } );

If the operation fails, the promise is rejected with a single parameter, a message key containing an error message as a string.

After creating a CMS object, you might want the user to be redirected to the corresponding page. For this, the scrivito.path_for_id JavaScript API method is available. It lets you generate a URL path for a given ID. The generated path does not take account of the slug or the permalink of the CMS object.

scrivito.create_obj({_obj_class: "Page"}).then(function(params) {  location.href = scrivito.path_for_id(params.id); });

The reverse method is available, too: scrivito.id_from_path extracts the CMS object ID from a given URL. It only recognizes URLs generated by scrivito.path_for_id Specifically, it does not recognize descriptive URLs that include a slug or represent a permalink. If scrivito.id_from_path cannot extract an ID, undefined is returned. Use it like this:

$.grep($('a'), function(a_tag) { return scrivito.id_from_path(a_tag.href) });

Deleting CMS objects: scrivito.delete_obj

scrivito.delete_obj(id) --> Promise()

This function deletes a CMS object in the current working copy. The id of the Obj to be deleted must be passed in as a string.

The function returns a jQuery Promise. Once the Obj has been deleted, the promise is fulfilled. If the operation fails, the promise is rejected with a single parameter, a message key containing an error message as a string.