A model instance usually represents a single entity record. See the class.
Sets an attribute or multiple attributes.
model.set('attributeName', value);
model.set({
attributeName1: value1,
attributeName2: value2,
});
// Options.
model.set(attributes, {
'silent': true, // suppresses 'change' events
});
You can pass custom options and check them in 'change' event listeners.
Gets an attribute.
const value = model.get('attributeName');
// all attributes
const attributes = Espo.Utils.cloneDeep(model.attributes);
Saves a model (to the back-end).
// Assuming model.id is set.
model.save()
.then(() => {
// callback on success
})
.catch(() => {
// callback on fail
});
Fetches a model (from the backend). Loads attribute values to the model. Returns a promise.
// assuming model.id is set
model.fetch()
.then(() => {
});
Get cloned attributes. Returns an object.
const attributes = model.getClonedAttributes();
Populate default values.
Sets field and link defs. May be needed if a model instantiated explicitly, not by the factory.
model.setDefs({
fields: {},
links: {},
};
string
A record ID.
string
An entity type.
string
A root API URL to use for syncing with the backend. For non-new records, an ID part will be appended.
string
An API URL to use for syncing with the backend. If specified, urlRoot will be omitted.
Record
Attribute values.
const name = model.attributes.name;
Model-factory is available in views. The model-factory allows to create a model instance of a specific entity type.
define('custom:views/some-custom-view', ['view'], (View) => {
return class extends View {
setup() {
// Use wait to hold off rendering until model is loaded.
this.wait(this.loadModel());
}
async loadModel() {
this.model = await this.getModelFactory().create('Account');
// entityType is set by the factory.
//const entityType = this.model.entityType;
this.model.id = this.options.id;
await model.fetch();
}
}
})
Instantiating w/o factory:
define('custom:views/some-custom-view', ['view', 'model'], (View, Model) => {
return class extends View {
setup() {
const model = new Model();
// URL will be used when fetching and saving.
model.urlRoot = 'MyModel';
model.id = 'someId';
this.wait(
// This performs `GET MyModel/someId` API call.
model.fetch();
);
}
}
})
Note: listenTo
and listenToOnce
are methods of the view class.
When model attributes get changed (not necessarily synced with backend).
this.listenTo(model, 'change', (model, options) => {
if (this.model.hasChanged('someAttribute')) {
// someAttribute is changed
}
if (options.ui) {
// changed via UI
// this options is set by field view
}
});
this.listenToOnce(model, 'change:someAttribute', (model, value, options) => {
// someAttribute is changed
});
Model synced with backend.
this.listenTo(model, 'sync', (model, response, options) => {
// synced with backend (fired after fetch or save)
});
Once model is removed (after DELETE request).
Defined in the application.
Once relationship panel updated. Available on detail/edit views.
This event is not fired. But you can fire it to update all relationship panels. Available on detail/edit views.
Passing model to a child view:
this.createView('someName', 'custom:views/some-view', {
model: this.model,
});