Sometimes we need to manipulate the data model in the client. In order to do this, backbone offers useful methods to access and filter the data on client-side.
bean = app.data.createBean('<NAME_OF_THE_MODULE>');
bean.set('<FIELD_NAME>', "<VALUE>");
bean.save();
let bean_collection = app.data.createBeanCollection('<NAME_OF_THE_MODULE>');
bean_collection.fetch({
fields: ["<field_to_be_returned>","<field_to_be_returned_2>"],
filter: [
{
"<FIELD_1>": {
"$equals": '<VALUE>'
},
"<FIELD_2>": {
"$lte": '<VALUE_2>'
},
}
],
success: function (data) {
console.log(data.models);
}
}
);
In the procedure above, we created a bean collection that will be used to retrieve a set of records from database based on a query we will run on the client-side.
In the filter parameter, we can specify any field which we want to filter by in order to retrieve records from the database. We have a list of operators to execute operations such as equal, different, and others.
Below, a list with all operators:
this.model.on("data:sync:complete", _.bind(this.fetchModel, this));
this.model.on("reset", _.bind(this.fetchModel, this));
this.model.on("change:pr_type", this.setType, this);
A data model has several events triggered when a specific action happens. The most common events are described below.
| The change event will be triggered even when the model is being synced for the first time. In order to avoid it, we should use the parameter inSync of the model |
Example:
fillDataFromParent: function()
{
if (!this.model.inSync) {
// code
}
}
fetchModel: function()
{
this.model.fetch();
}
this.model.getRelatedCollection("<NAME_OF_THE_RELATIONSHIP_1>").on("data:sync:complete", _.bind(this.fetchModel, this));
this.model.getRelatedCollection("<NAME_OF_THE_RELATIONSHIP_2>").on("reset", _.bind(this.fetchModel, this));
this.model.getRelatedCollection("<NAME_OF_THE_RELATIONSHIP_3>").on("reset", _.bind(this.fetchModel, this));
When a record is loaded the main model holds all related model used by subpanels in the record view.
In the code above, we can access a related model used to populate a subpanel. This related collection might not be populated and, in order to populate it, the method fetch should be called.
Contact us using the form below if you have any questions