parseIdAttribute

method
 parseIdAttribute() 

Option name Type Description
url

Parses Id attribute from url

parseIdAttribute: function(url) {
    var found = url.match(/:([a-zA-z0-9_]+)/);
    if (found instanceof Array && found.length > 1) {
        return found[1];
    }
    return null;
},

get

method
 get() 

Option name Type Description
name string

Attribute name

return array

Gets attribute by name

get: function(name) {
    return this.data[name] || this.defaults[name];
},

set

method
 set() 

Option name Type Description
name string

Attribute name

value any

Attribute value

Sets attribute value by name

set: function(name, value) {
    this.data[name] = value;
    this.updateCollection();
    this.trigger('change', name);
},

getEndpointUrl

method
 getEndpointUrl() 

Option name Type Description
return string

item url

Gets item url

getEndpointUrl: function() {
    var id = this.data[this.idAttribute];
    if (this.url) {
        if (id) {
            return this.url.replace(':' + this.idAttribute, id);
        }
        return this.url;
    } else {
        if (id) {
            return this.urlRoot + '/' + id;
        }
        return this.urlRoot;
    }
},

fetch

method
 fetch() 

Option name Type Description
query object

Request query

return Promise

Response promise

Gets data from the sever

fetch: function(query) {
    var that = this,
        url = this.getEndpointUrl();
    url +=  owl.ajax.toQueryString(query);
    return owl.ajax.request({
        url: url,
        type: 'GET'
    })
    .then(function(result) {
        that.data = result;
        that.updateCollection();
        that.trigger('change', Object.keys(that.data));
        return result;
    });
},

clear

method
 clear() 

Removes all attributes from the model

clear: function() {
    this.data = {};
    this.updateCollection();
},

save

method
 save() 

Option name Type Description
query object

Request query

return Promise

Response promise

Save a model to database

save: function(query) {
    var that = this;
    var url = this.getEndpointUrl();
    var id = this.data[this.idAttribute];
    if(id) {
        url += '/' + this.data[this.idAttribute];
    }
    return owl.ajax.request({
        url: url + owl.ajax.toQueryString(query),
        type: id ? 'PUT' : 'POST',
        data: this.data
    })
    .then(function(result) {
        if(result[that.idAttribute]) {
            that.data[that.idAttribute] = result[that.idAttribute];
        }
        that.updateCollection();
        that.trigger('change', [that.idAttribute]);
        return result;
    });
},

update

method
 update() 

Option name Type Description
data object

Data to update

query object

Request query

return Promise

Response promise

Updates local data and saves model

update: function(data, query) {
    var that = this;
    var id = this.data[this.idAttribute];
    if(!id) {
        return new Promise(function(resolve, reject) {
            reject(new Error('Can not update model without id'));
        });
    }
    this.data = owl.util.extend(this.data, data, true);
    return this.save(query).then(function(result) {
        that.updateCollection();
        that.trigger('change', Object.keys(data));
        return result;
    });
},

patch

method
 patch() 

Option name Type Description
data object

Data to patch

query object

Request query * @param {string} path Additional path

return Promise

Response promise

Partially updates model

patch: function(data, query, path) {
    var that = this;
    var url = this.getEndpointUrl();

    if (typeof path === 'string') {
        url += path;
    }

    if (typeof query === 'object' && query !== null) {
        url += owl.ajax.toQueryString(query);
    }

    this.data = owl.util.extend(this.data, data, true);
    return owl.ajax.request({
        url: url,
        type: 'PATCH',
        data: data
    }).then(function(result) {
        that.updateCollection();
        that.trigger('change', Object.keys(data));
        return result;
    });
},

updateCollection

method
 updateCollection() 

Updates collection data

updateCollection: function() {
    if(this.collection) {
        this.collection.update(this.collectionIndex);
    }
},

destroy

method
 destroy() 

Option name Type Description
query object

Request query

return Promise

Response promise

Remove a model

destroy: function(query) {
    var that = this;
    var id = this.data[this.idAttribute];
    if (!id) {
        return new Promise(function(resolve, reject) {
            reject(new Error('Can not destroy model without id'));
        });
    }
    return owl.ajax.request({
        url: this.getEndpointUrl() + owl.ajax.toQueryString(query),
        type: 'DELETE'
    }).then(function(result) {
        that.clear();
        return result;
    });
},

getData

method
 getData() 

Option name Type Description
return object

Model data

Gets models data

getData: function() {
    return this.data;
},

setData

method
 setData() 

Set model data

setData: function(data) {
    this.data = data;
    this.updateCollection();
    this.trigger('change');
},

getCollection

method
 getCollection() 

Option name Type Description
return owl.Collection

Model collection

Gets model collection

getCollection: function() {
    return this.collection;
},

getCollectionIndex

method
 getCollectionIndex() 

Option name Type Description
return number

Model collection index

Gets model collection index

getCollectionIndex: function() {
    return this.collectionIndex;
},

on

method
 on() 

Option name Type Description
event string

Event name

listener function

Event listener

Adds event listener

on: function(event, listener) {
    if (!this.events[event]) {
        this.events[event] = [];
    }
    this.events[event].push(listener);
},

off

method
 off() 

Option name Type Description
event string

Event name

listener function

Event listener

Removes event listener

off: function(event, listener) {
    if (!event) {
        this.events = [];
    } else if (!listener) {
        delete this.events[event];
    } else if (this.events[event]) {
        this.events[event] = this.events[event].filter(function(currentListener) {
            return currentListener !== listener;
        });
    }
},

triggerSingle

method
 triggerSingle() 

Option name Type Description
event string

Event name

Trigger single event

triggerSingle: function(event) {
    var listeners = this.events[event];
    if(this.collection) {
        this.collection.trigger(event);
    }
    if (listeners) {
        listeners.forEach(function(listener) {
            listener();
        });
    }
},

trigger

method
 trigger() 

Option name Type Description
event string

Event name

subEvents array

Sub events array

Triggers events

trigger: function(event, subEvents) {
    var that = this;
    this.triggerSingle(event);
    if(subEvents && subEvents instanceof Array) {
        subEvents.forEach(function(subEvent) {
            that.triggerSingle(event + ':' + subEvent);
        });
    } else if (subEvents) {
        this.triggerSingle(event + ':' + subEvents);
    }
}
    };
    owl.Model = Model;
})(window, owl);