Ireland Flag Contact Icon +353 1 693 0000   UK Flag Contact Icon +44 203 411 6799

Working with Model in the Client

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.

Create a new model and save it

bean = app.data.createBean('<NAME_OF_THE_MODULE>');
bean.set('<FIELD_NAME>', "<VALUE>");
bean.save();
  • NAME_OF_THE_MODULE: Name of the module you want to create a record.
  • FIELD_NAME: Name of the field you want to set a value.
  • VALUE: Value to be assigned fo a field.

Filter methods in the model collection in the client

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.

  • NAME_OF_THE_MODULE: Name of the module you want to create a record.
  • FIELD_TO_BE_RETURNED: Fields that should be returned in the model.
  • FIELD_1, FIELD_2: Fields that will be used for filtering

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:

  • $equals: the value of the field should de equal to a specified value
  • $not_equals: the value of the field should not be equal to a specified value

Used for text fields

  • $starts: the value of the field should start with a specified value (used for varchar field)

Used for enum fields

  • $in: the value of the field should match one of the values in the list
  • $not_in: the value of the field should not match the values specified in the list

Used for multi enum fields

  • $contains: list field should contain one of the values in the list
  • $not_contains: list field should not contain the values specified in the list

Used only for numeric fields

  • $gt: the value of the field should be greater than a specific value
  • $lt: the value of the field should be lower to a specific value
  • $gte: the value of the field should be greater or equal than a specific value
  • $lte: the value of the field should be lower or equal than a specific value
  • $between: the value of the field should contain a specific value within a specific range

Useful listeners functions in the model

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.

  • data:sync:complete: Event triggered when the model is completed sync.
  • reset: Event triggered to fix when a client model is not synchronized to the server. Following an update on the client-side only. For example, when a record is removed from a client model.
  • sync: Event triggered for every sync operation made by the model.
  • change:field_name: Event triggered when the value of one specific field is changed.
  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
    }
}

Access related model from a current model

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.

Be Careful

  • It is usually not populated when a subpanel is closed.
  • This related collection will only bring records displayed on the subpanel associated.

Contact us using the form below if you have any questions



Guilherme

Latest posts