nci/static/js/app/stores/builds.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-05-07 21:55:40 +00:00
'use strict';
define([
'underscore',
'reflux', 'app/actions/build', 'app/resources'
], function(_, Reflux, BuildActions, resources) {
var resource = resources.builds;
var Store = Reflux.createStore({
listenables: BuildActions,
builds: [],
2015-09-27 20:47:50 +00:00
getInitialState: function() {
return this.builds;
},
onChanged: function(data) {
2015-05-10 10:04:54 +00:00
var oldBuild = _(this.builds).findWhere({id: data.buildId});
2015-05-07 21:55:40 +00:00
if (oldBuild) {
2015-05-10 10:04:54 +00:00
_(oldBuild).extend(data.changes);
2015-05-07 21:55:40 +00:00
} else {
2015-05-10 10:04:54 +00:00
this.builds.unshift(
_({id: data.buildId}).extend(data.changes)
);
2015-05-07 21:55:40 +00:00
}
this.trigger(this.builds);
},
onCancelled: function(data) {
// WORKAROUND: client that trigger `onCancel` gets one `onCancelled`
// call other clients get 2 calls (second with empty data)
if (!data) {
return;
}
var index = _(this.builds).findIndex({id: data.buildId});
if (index !== -1) {
this.builds.splice(index, 1);
}
this.trigger(this.builds);
},
2015-05-07 21:55:40 +00:00
init: function() {
resource.subscribe('change', this.onChanged);
resource.subscribe('cancel', this.onCancelled);
2015-05-07 21:55:40 +00:00
},
2015-07-09 20:12:24 +00:00
onReadAll: function(params) {
2015-05-07 21:55:40 +00:00
var self = this;
2015-07-09 20:12:24 +00:00
resource.sync('readAll', params, function(err, builds) {
2015-05-15 05:43:57 +00:00
if (err) throw err;
self.builds = builds;
self.trigger(self.builds);
2015-05-07 21:55:40 +00:00
});
2015-05-17 13:48:16 +00:00
},
onCancel: function(buildId) {
resource.sync('cancel', {buildId: buildId}, function(err) {
if (err) throw err;
});
}
2015-05-07 21:55:40 +00:00
});
return Store;
});