Skip to content

Instantly share code, notes, and snippets.

@mockdeep
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mockdeep/24397e83380fceb8e77c to your computer and use it in GitHub Desktop.
Save mockdeep/24397e83380fceb8e77c to your computer and use it in GitHub Desktop.
'use strict';
var _ = require('lodash');
var request = require('../helpers').request;
module.exports = {
models: [],
loaded: false,
on: function (event, callback) {
this.callbacks = this.callbacks || {};
this.callbacks[event] = this.callbacks[event] || [];
this.callbacks[event].push(callback);
},
off: function (event, callback) {
this.callbacks[event] = _.without(this.callbacks[event], callback);
},
trigger: function (event) {
if (!this.callbacks || !this.callbacks[event]) { return; }
this.callbacks[event].map(function (callback) { callback(); });
},
url: function () {
return '/' + this.name + 's';
},
load: function () {
request({
method: 'get',
url: this.url(),
success: this.updateModels.bind(this)
});
},
unload: function () {
this.loaded = false;
this.trigger('change');
},
updateModels: function (data) {
this.models = data[this.name + 's'];
this.loaded = true;
this.trigger('change');
},
getAll: function () {
if (this.loaded) {
var data = {};
data[this.name + 's'] = this.models;
return new Promise(function (resolve) { resolve(data); });
} else {
return request({
method: 'get',
url: this.url(),
success: this.updateModels.bind(this)
});
}
},
create: function (attrs) {
var data = {};
data[this.name] = attrs;
return request({
url: this.url(),
method: 'post',
data: data,
success: this.unload.bind(this)
});
},
update: function (id, attrs) {
var data = {};
data[this.name] = attrs;
return request({
url: this.url() + '/' + id,
data: data,
success: this.unload.bind(this)
});
},
destroy: function (id) {
return request({
url: this.url() + '/' + id,
method: 'delete',
success: this.unload.bind(this)
});
}
};
'use strict';
var _ = require('lodash');
var RestfulStore = require('./restful_store');
module.exports = _.extend({}, RestfulStore, {name: 'task'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment