Skip to content

Latest commit

 

History

History
154 lines (123 loc) · 3.34 KB

xone_model.md

File metadata and controls

154 lines (123 loc) · 3.34 KB

Xone Active Model

Define Model

app/js/model/record.js

var Record = MODEL.register('Record', (function(){

    /**
     * @constructor
     * @param {Object<string, *>} data
     * @struct
     */

    function RecordClass(data){

        this.id = data.id;
        this.title = data.title;
        this.date = data.date;
        
        /* A model can include further models */

        this.user = MODEL.User.new(data.user);
        this.image = MODEL.Media.new(data.image);
    }
    
    /* Define Prototype (Shared Functions) */
    
    RecordClass.prototype.showID = function(){
        
        console.log(this.id); // 'this' points to a record
    };
    
    return RecordClass;
})());

/* Define Helpers: */

Record.msg = function(str){

    console.log(str);
};

Record.helperID = function(record){

    this.msg(record.id); // 'this' points to the corresponding model class of a record
};

Prototypes VS. Helpers

The decision of where the method has to be defined is simple: if the method deals with a record you should use prototype (where 'this' points to a record), if the method deals with the model you should define it as a helper (where 'this' points to a model).

Working with Active Models

Create from passed params

var record = MODEL.Record.new({
    id: 1,
    title: 'record',
    date: new Date()
});

Same as:

var record = MODEL.new('Record', {
    id: 1,
    title: 'record',
    date: new Date()
});

Call methods:

// call the prototype method from the example above:
record.showID(); // --> 1

// call the model helper method from the example above:
MODEL.Record.helperID(record); // --> 1

Edit and saves the model to the storage:

Record.title = 'New title';
Record.save();

Find a model

// find by id;
var record = MODEL.Record.find(8);

// find by one custom attribute;
var record = MODEL.Record.findBy('title', 'My Record');

Find multiple models by a custom query;

var result = MODEL.Record.where({
    visibility: 'public'
});

Find multiple models by a custom filter;

var result = MODEL.Record.where(function(){
    return this.start > 123673242 && this.end < 123774241;
});

Find multiple models by a like-query;

var result = MODEL.Record.like({
    visibility: ' PuBLic ' // --> matches 'public'
});

Get all entries from a model:

var all_records = MODEL.Record.all();

Get entries count from a model:

var count = MODEL.Record.count();

Get ranged entries from a model:

var result = MODEL.Record.range(3);    // max 3
var result = MODEL.Record.range(0, 3); // 0, 1 and 2

Update entry and save to storage:

var record = MODEL.Record.find(8).update({title: 'New Title'}).save();

Delete entry: (will also delete from storage)

MODEL.Record.find(8).delete();

Sorting models by a custom filter:

MODEL.Record.sort(function(a, b){
    return a.date > b.date;
});

Batch through a list:

MODEL.Record.newFromList([/* Array of Record Params */]);
MODEL.Record.createFromList([/* Array of Record Params */]);
MODEL.Record.uppdateAll([/* Array of Records */], { date: new Date() });
MODEL.Record.saveAll([/* Array of Records */]);
MODEL.Record.deleteAll([/* Array of Records */]);