Model
Models are the basic data object in JointJS. They are a discrete chunk of data and a bunch of useful, related methods for performing computations and transformations on that data. dia.Cell
extends mvc.Model
.
Propertiesβ
attributesβ
model.attributes
The attributes
property is the internal hash containing the model's state. Please use set()
to update the attributes instead of modifying them directly.
Due to the fact that Events accepts space separated lists of events, attribute names should not include spaces.
changedβ
model.changed
The changed property is the internal hash containing all the attributes
that have changed since its last set()
. Please do not update changed directly since its state is internally maintained by set()
. A copy of changed can be acquired from changedAttributes
.
cidβ
model.cid
A special property of models, the cid
or client id is a unique identifier automatically assigned to all models when they're first created.
cidPrefixβ
model.cidPrefix
If your model has an id
that is anything other than an integer or a UUID, there is the possibility that it might collide with its cid
. To prevent this, you can override the prefix that cids
start with.
idβ
model.id
A special property of models, the id
is an arbitrary string (integer id or UUID). If you set the id
in the attributes hash, it will be copied onto the model as a direct property. model.id
should not be manipulated directly, it should be modified only via model.set('id', β¦)
.
idAttributeβ
model.idAttribute
A model's unique identifier is stored under the id
attribute. If you're directly communicating with a backend (MongoDB) that uses a different unique key, you may set a Model's idAttribute
to transparently map from that key to id
. If you set idAttribute
, you may also want to override cidPrefix
.
const Shape = mvc.Model.extend({
idAttribute: "_id"
});
const shape = new Shape({ _id: 1, name: "Rectangle" });
console.log("Shape id: " + shape.id); // Shape id: 1
validationErrorβ
model.validationError
The value returned by validate
during the last failed validation.
Methodsβ
changedAttributes()β
model.changedAttributes([attributes])
Retrieve a hash of only the model's attributes that have changed since the last set()
, or false
if there are none. Optionally, an external attributes
hash can be passed in, returning the attributes in that hash which differ from the model. This can be used to figure out which portions of a view should be updated.
clear()β
model.clear([options])
Removes all attributes from the model, including the id
attribute. Fires a "change"
event unless silent
is passed as an option.
clone()β
model.clone()
Returns a new instance of the model with identical attributes.
defaults()β
model.defaults or model.defaults()
The defaults hash (or function) can be used to specify the default attributes for your model. When creating an instance of the model, any unspecified attributes will be set to their default value.
Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances. Instead, define defaults as a function.
extend()β
mvc.Model.extend(properties, [classProperties])
To create a Model
class of your own, you can extend mvc.Model
. Provide instance properties, and optional classProperties to be attached directly to the constructor function.
extend
correctly sets up the prototype chain, so subclasses created with extend
can be further extended and subclassed.
const BaseShape = mvc.Model.extend({
initialize: function() {...}
});
const Shape = BaseShape.extend({...});
Brief aside on super
: JavaScript does not provide a simple way to call super β the function of the same name defined higher on the prototype chain. If you override a core function like set
, and you want to invoke the parent object's implementation, you'll have to explicitly call it, along these lines:
const Shape = mvc.Model.extend({
set: function(attributes, options) {
mvc.Model.prototype.set.apply(this, arguments);
...
}
});
get()β
model.get(attribute)
Get the current value of an attribute from the model. For example: model.get("title")
. get()
doesn't provide nesting capability in the form of a string. That means any path representation is considered to be one attribute.
has()β
model.has(attribute)
Returns true
if the attribute is set to a non-null or non-undefined value.
hasChanged()β
model.hasChanged([attribute])
Has the model changed since its last set()
? If an attribute is passed, returns true
if that specific attribute has changed.
Note that this method is only useful during the course of a "change"
event.
shape.on("change", function() {
if (shape.hasChanged("title")) {
...
}
});
initialize()β
If the model defines an initialize function, it will be invoked when the model is created. Initialize is an empty function by default. Override it with your own initialization logic.