Collection

constructor
 Collection() 

Option name Type Description
data array

Collection data

options object

Collection options

owl.Collection

function Collection(data, options){
    this.url = options.url;
    this.model = options.model;
    this.data = [];
    this.models = [];
    this.events = {};

    this.setData(data);
}
Collection.prototype = {

fetch

method
 fetch() 

Option name Type Description
query object

Query that will be passed with request

Gets data from server

fetch: function(query) {
    var that = this;
    return owl.ajax.request({
        url: this.url + owl.ajax.toQueryString(query),
        type: 'GET'
    })
    .then(function(result) {
        that.setData(result);
        return result;
    });
},

clear

method
 clear() 

Removes models from collection

clear: function() {
    this.data = [];
    this.models = [];
    this.length = 0;
    this.trigger('change');
},

setData

method
 setData() 

Option name Type Description
data object

Collection data

Sets collection data

setData: function(data) {
    var that = this;
    if (data instanceof Array) {
        this.data = data;
        this.length = data.length;
        this.models = data.map(function(item, index) {
            return new that.model(item, {
                collection: that,
                collectionIndex: index
            });
        });
    } else {
        this.models = [];
        this.length = 0;
    }
    that.trigger('change');
},

getData

method
 getData() 

Option name Type Description
return array

Collection data

Gets collection data

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

getModels

method
 getModels() 

Option name Type Description
return array

Collection models

Gets collection models

getModels: function() {
    return this.models;
},

getLength

method
 getLength() 

Option name Type Description
return number

Gets collection length

getLength: function() {
    return this.length;
},

get

method
 get() 

Gets a model by index

get: function(index) {
   return this.models[index];
},

update

method
 update() 

Updates collection data

update: function(index) {
    if (typeof index === 'number') {
        this.data[index] = this.models[index].getData();
    } else {
        this.data = this.models.map(function(model) {
            return model.getData();
        });
    }
},

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;
        });
    }
},

trigger

method
 trigger() 

Option name Type Description
event string

Event name

Triggers event

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