Skip to content

Instantly share code, notes, and snippets.

@orionstar
Created October 10, 2018 17:24
Show Gist options
  • Save orionstar/0e970dc8e2346c6e640746fbb74a7d1f to your computer and use it in GitHub Desktop.
Save orionstar/0e970dc8e2346c6e640746fbb74a7d1f to your computer and use it in GitHub Desktop.
function BaseModel () {};
BaseModel.prototype = {
constructor: BaseModel,
_items: [],
_dataSource: '',
// ------------------------------------------------------------------------
_init: function (dataSource) {
this._items = [];
this._dataSource = '';
this._dataSource = dataSource;
this.sync();
},
// ------------------------------------------------------------------------
_addItem: function (item) {
item.cid = 'c' + parseInt(Math.random() * 10000000000000);
this._items.push(item);
return item;
},
// ------------------------------------------------------------------------
getItem: function (parameter, value) {
return _.find(this._items, function (item) {
return item[parameter] === value;
});
},
// ------------------------------------------------------------------------
getItemByCid: function (cid) {
return this.getItem('cid', cid);
},
// ------------------------------------------------------------------------
getAll: function () {
return this._items;
},
// ------------------------------------------------------------------------
sync: function () {
var self = this;
$.ajax({
url: self._dataSource,
type: 'get',
dataType: 'json'
})
.done(function(items) {
_.each(items, function (item) {
self._addItem(item);
});
})
.fail(function() {
console.error('Szinkronizális hiba:', self);
});
}
// ------------------------------------------------------------------------
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment