Skip to content

Defining a Model

Marcus Krejpowicz edited this page Sep 20, 2013 · 12 revisions

This page explains how to define the schema of a model and specify validators. Note : The same techniques can be used for defining entities.

An example model definition for a person can look like this:

define(["js/data/Model", "js/data/validator/EmailValidator", "app/entity/Address", "js/data/Collection", "app/model/Relation"], function(Model, EmailValidator, Address, Collection, Relation){

  return Model.inherit('app.model.Person',{
    schema: {
       name: String,
       birthDate: Date,
       email: String,
       // A list of Address entities
       // Whole data is saved in Person model
       addresses: {
         required: false,
         type: [Address]
       },
       // A collection of linked Relations
       // Saves only the links to the Relations in the Person model
       relations: {
         required: false,
         type: Collection.of(Relation)
       }
    },
    validators: [
      new EmailValidator({
        field: "email"
      })
    ]
  });

});

The Schema object

The schema object defines the existing fields and their types. The example above shows the short written version. If you want to define more than the type you can set an object with the following options

  • type: DateString | Number | Boolean | ModelFactory | 'app.model.SubModel'[Factory]Collection.of(ModelFactory)
  • required: true | false if the field is required (will be checked by the SchemaValidator)
  • generated: true | false if the field is generated by the server/db side
  • includeInIndex: true| false if the field should be included in the collection payload (needed for server side)

The schema object gets automatically extended if you inherit from a super model.

The ID field

The default ID field in rAppid.js is "id". If you want to use another field you can set this by the idField property.

Model.inherit('app.model.Person',{
    schema: {
       birthDate: Date,
       email: String
    }
    idField: "login"
  });

The idField is automatically added to the schema with the options required = true and generated = true.

The updated and created fields

Every model automatically get's an updated and created field which are from type Date and are generated by the server. If you don't need this fields you can disable them by setting the updatedField and/or the createdField to false.

Model.inherit('app.model.Person',{
    schema: {
       birthDate: Date,
       email: String
    }
    createdField: false // default is "created",
    updatedField: false // default is "updated"
  });

The href field

When talking to a REST-API the payload includes a href which can also be part of the model. The default hrefField is href. You can override this by defining another hrefField.

Validators

Every model has an internal SchemaValidator which checks if the value is set and if the type of the value is the one in the schema. Additional model or field validators can be specified inside the validators property.
To specify which field the validator has to validate, you can set the field attribute in the validator.

rAppid.js comes with an EmailValidator and an RegExValidator. You can create your own validators by inheriting from js.data.validator.Validator. This validator has a public and a protected validate method. The public validate method is asynchronous. If you are planning to write an asynchronous validation like checking an email address against a server override the public validate method. Synchronous validations can be implemented by overriding the proctected _validate method.

Clone this wiki locally