-
Notifications
You must be signed in to change notification settings - Fork 0
Models
Models represent your data from a server or from a client.
All your models should be inherited from Sirius.BaseModel class.
Sirius.BaseModel contains a set of useful properties and methods for comfortable work with data.
attrs - model properties, for every property Sirius will be generated setter and getter.
class Post extends Sirius.BaseModel
@attrs: ['id', {'title': 'my-default-title'}]
# create
post = new Post({id: 'my-id'})
# getters
post.id() # => 'my-id'
post.title() # => 'my-default-title'
# setters
post.title("another title")
post.title() # => another title
skip - ignore extra keys, when creating a model, by default is false. That is useful for serialization. When your
the model does not match with backend response object (more fields for example)
class MyModel extends Sirius.BaseModel
@attrs: ['id']
class MyModel1 extends Sirius.BaseModel
@attrs: ['id']
@skip: true
obj = {'id': 1, 'foo': "bar"}
new MyModel(obj) # => error
new MyModel1(obj) # => ok
guid_for - generate guid for an attribute
class Post extends Sirius.BaseModel
@attrs: ['id']
@guid_for: 'id' # or ['id']
post = new Post()
post.id() # => 75b659d2-a40d-dd41-9335-81473e1c5a8d
validate - define validators for attributes in a model
class Post extends Sirius.BaseModel
@attrs: ["id", "title", "description"]
@validate :
id:
presence: true,
numericality: only_integers: true
inclusion: within: [1..10]
title:
exclusion: within: ["Title"]
description:
custom: true
validate_with: (desc) ->
if desc == "foo"
true
else
@msg = "Description must be foo"
false
comp - define computed fields, computed fields are available in validate too.
class MyModel extends Sirius.BaseModel
@attrs: ['first_name', 'last_name']
@comp('full_name', 'first_name', 'last_name')
@comp('nick', (first, last) -> "#{first.charAt(0)}.#{last}")
@comp('sum', 'full_name', 'nick')
my_model = new MyMode()
my_model.full_name() # => null
my_model.first_name('john')
my_model.full_name() # => null
my_model.last_name('doe')
my_model.full_name() # => john doe
my_model.nick() # => j.doe
my_model.sum() # => john doe j.doe
get_attributes - return an array of attributes for a model
reset - reset attributes to nulls
is_valid(attr = null) - check that model is valid for save or update or attribute in model is valid
save - return true if model is valid, otherwise return false
to_json - convert model to json representation
to_object - convert model to javascript object
@from_json - convert JSON to model
json = ...
Post.from_json(json) # => post modelcompare - compare two models, you need to redefine this method in your model
class Post extends Sirius.BaseModel
@attrs: ['id', 'title']
compare: (other) ->
@id() == other.id()clone - clone current model
set_error - set error to attribute validator
model.set_error("id.presence", "some error") get_error(attr = null) - get all errors, or exactly error for attribute
model.get_error("id") # ["some error"]@register_validator - add new validator for models, see validators page