Class: Model

Oracle® Fusion Middleware Oracle JavaScript Extension Toolkit (JET)
12c (12.1.4)

E54107-01

QuickNav

oj. Model

Object representing name/value pairs for a data service record

Constructor

new Model(attributes, options)

Parameters:
Name Type Argument Description
attributes Object <optional>
Initial set of attribute/value pairs with which to seed this Model object
options Object <optional>
collection: collection for this model
Source:

Mixes In

Fields

attributes :Object

Attribute/value pairs held by the Model.
Source:

customURL :function(string,Object):(string|null)|null

A callback to allow users to completely customize the data service URLs The callback has the following parameters: operation (String): one of create, read, update, patch, or delete indicating the type of operation for which to return the URL

model (Object): the oj.Model object requesting the URL

options (Object) : one or more of the following properties:

recordID : id of the record involved, if relevant

customURL functions should return either: null, in which case the default will be used; a string, which will be used with the standard HTTP method for the type of operation, or an Object with any ajax attributes. This must at minimum include the URL:

url: giving the custom URL string

type: (optional) a string indicating the type of HTTP method to use (GET, POST, DELETE, etc.)

(other): (optional) any other ajax attributes to pass in the ajax call

Source:

defaults :Object

The set of attribute/value pairs that serve as default values when new Model objects are created.
Source:

id :String

The Model's unique ID. This can be set by the application or retrieved from the data service. This ID will be appended to the URL for single-record data operations (update, delete).
Source:

idAttribute :String

The name of the model property to be used as the unique ID. See property id. This defaults to a value of "id".
Source:

urlRoot :String

The base url on the data service used to perform CRUD operations on models. If not defined, the Model will look to its collection. One or the other must be defined before CRUD operations can succeed.
Source:

Methods

<static> extend(properties, classProperties) → {function(new:Object, ...)}

Create a new, specific type of Model object to represent single records from a JSON data set.
This:
  • {Object}
Parameters:
Name Type Argument Description
properties Object <optional>
Properties for the new Model class.

defaults: an Object containing starting attribute/value pairs for some or all of the record's potential attributes

parse: a user callback function to allow parsing of JSON record objects as they are returned from the data service

parseSave: a user callback function to allow conversion of Models back into a format appropriate for the data service on save calls

urlRoot: the URL to use to get records from the data service in the abscence of a collection (when an id is appended)

initialize: a user callback function to be called when this model is created

validate: a user callback function that will be called before a save to the data service occurs. The callback is passed the current set of attributes and save options.

classProperties Object <optional>
properties that attach to the whole class
Source:
Returns:
new Model object
Type
function(new:Object, ...)

['sync'](method, model, options) → {Object}

Performs communications with the server. Can be overridden/replaced by clients
Parameters:
Name Type Argument Description
method string "create", "read", "update", or "delete"
model Object Model to be read/saved/deleted/created
options Object <optional>
to control sync success: called if sync succeeds error: called if sync fails others are passed to jQuery
Source:
Returns:
xhr object
Type
Object

_setProp(prop, value, options) → {boolean}

Parameters:
Name Type Argument Description
prop Object | | string
value Object
options Object <optional>
Source:
Returns:
Type
boolean

change()

Fire attribute change events to all registered listeners.
Source:

changedAttributes(attributes) → {Object||boolean}

Return the set of attributes and values that have changed since the last fetch, or since the Model object was created. If attribute/value pairs are passed in, check those to see if they're different than the model. Return false if there were no changes
Parameters:
Name Type Argument Description
attributes Object <optional>
One or more attribute/value pairs to check against the model for changes
Source:
Returns:
the set of all attribute value pairs that have changed since last update or save, if no attributes passed in; the set of all attribute value pairs that are different than those listed in the attributes parameter, if present. False if no changes
Type
Object | | boolean

clear(options) → {Object||boolean}

Parameters:
Name Type Argument Description
options Object <optional>
silent: if true, do not fire the change event validate: if true, validate the unsetting of all properties
Source:
Returns:
the Model, or false if validation on clear fails
Type
Object | | boolean

clone()

Source:

destroy(options)

Delete the record represented by this Model object from the data service.
Parameters:
Name Type Argument Description
options Object <optional>
Options for the destroy operation. They include success and/or error callback function upon the delete or failure of the delete.
Source:

fetch(options)

Loads the Model object from the data service URL. Performs a data "read."
Parameters:
Name Type Argument Description
options Object <optional>
Options to control fetch

success: a user callback called when the fetch has completed successfully. This makes the fetch an asynchronous process. The callback is called passing the Model object, raw response, and the fetch options argument.

error: a user callback function called if the fetch fails. The callback is called passing the model object, xhr, and options arguments.

Source:

get(property) → {Object}

Returns the value of the property from the Model.
Parameters:
Name Type Description
property string Property to get from model
Source:
Returns:
value of property
Type
Object

has(property) → {boolean}

Determines if the Model has a certain property set, vs. undefined.
Parameters:
Name Type Description
property string Property to check for
Source:
Returns:
true if the model contains the given property
Type
boolean

hasChanged(attribute) → {boolean}

Return true if the Model object has had any changes made to its values, or if any changes have been made to the optional set of attributes passed in.
Parameters:
Name Type Argument Description
attribute Array <optional>
One or more attributes to check for changes
Source:
Returns:
true if the Model object has had any changes since retrieval or last update at all (if no attributes parameter); true if the Model object has had changes to one or more of the passed-in attributes since retrieval or last update (if attributes parameter present).
Type
boolean

invert() → {Object}

Source:
Returns:
returns the model's value/attribute pairs as an array
Type
Object

isNew() → {boolean}

Determines if this Model object has been assigned an id value yet. This indicates whether or not the Model's data has been saved to or fetched from the data service at any point.
Source:
Returns:
true if the Model object has not had its id set yet, false if not.
Type
boolean

isValid() → {boolean}

Source:
Returns:
true if validate passes or if no validate callback
Type
boolean

keys() → {Array}

Source:
Returns:
array of all the model's attributes
Type
Array

omit(keys) → {Object}

Parameters:
Name Type Description
keys Array | | Object keys to exclude from the returned attribute/value pairs
Source:
Returns:
array of the model's attribute/value pairs except those listed in keys
Type
Object

pairs() → {Object}

Source:
Returns:
returns the model's attribute/value pairs as an array
Type
Object

pick(keys) → {Object}

Parameters:
Name Type Description
keys Array | | Object keys for which to return attribute/value pairs
Source:
Returns:
array of the model's attribute/value pairs filtered by keys
Type
Object

previous(attr) → {Object}

Parameters:
Name Type Description
attr string
Source:
Returns:
previous value of attr, if any. If the attribute has not changed, returns undefined
Type
Object

previousAttributes() → {Object}

Source:
Returns:
a copy of the model's previous attributes
Type
Object

previousChanges() → {Object}

Source:
Returns:
an object containing all the previous values of chnaged attributes
Type
Object

save(attributes, options) → {Object|boolean}

Saves the current Model object to the data service. Performs a data "update."
Parameters:
Name Type Argument Description
attributes Object <optional>
One or more attribute name/value pairs to set on the Model before the save.
options Object <optional>
Options to control save

success: a user callback called when the save has completed successfully. This makes the save an asynchronous process. The callback is called passing the Model object, response from the AJAX call, and the fetch options argument.

error: a user callback function called if the save fails. contextType: in case the user's REST service requires a different POST content type than the default, 'application/json' validate: should the validation routine be called if available patch: should only changed attributes be sent via a PATCH?

Source:
Returns:
returns false if validation failed
Type
Object | boolean

set(property, value, options) → {Object||boolean}

Set the value(s) of one or more attributes of the model, and fire events.
Parameters:
Name Type Argument Description
property string | | Object Property attribute name to set, or an Object containing attribute/value pairs
value Object <optional>
Value for property if property is not an Object containing attribute/value pairs
options Object <optional>
Options may be passed in including "silent" to prevent events from firing, or "unset" to delete all the properties passed in rather than setting them
Source:
Returns:
the model itself, false if validation failed on set
Type
Object | | boolean

toJSON() → {Object}

Return a copy of Model's current attribute/value pairs
Source:
Returns:
a copy of the Model's current set of attribute/value pairs.
Type
Object

unset(property, options) → {boolean}

Deletes the given property from the Model.
Parameters:
Name Type Argument Description
property string Property to remove from model
options Object <optional>
do not fire change events if "silent" is set to true
Source:
Returns:
false if validation of the unset fails
Type
boolean

url() → {string|null}

Source:
Returns:
url to access this model in the data source
Type
string | null

values() → {Array}

Source:
Returns:
array of all the model's attributes values
Type
Array