1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/UI/Commands/CommandController.js

67 lines
2.0 KiB
JavaScript
Raw Normal View History

2013-06-22 09:24:24 +03:00
'use strict';
2013-09-11 09:33:47 +03:00
define(
[
'app',
2013-09-11 09:33:47 +03:00
'Commands/CommandModel',
'Commands/CommandCollection',
2013-09-12 03:42:15 +03:00
'underscore',
'jQuery/jquery.spin'
], function (App, CommandModel, CommandCollection, _) {
2013-05-12 02:39:32 +03:00
var singleton = function () {
2013-09-12 03:42:15 +03:00
return {
2013-09-11 09:33:47 +03:00
Execute: function (name, properties) {
2013-06-13 05:55:11 +03:00
var attr = _.extend({name: name.toLocaleLowerCase()}, properties);
2013-09-11 09:33:47 +03:00
var commandModel = new CommandModel(attr);
2013-09-12 03:42:15 +03:00
return commandModel.save().success(function () {
CommandCollection.add(commandModel);
});
},
2013-09-12 03:42:15 +03:00
bindToCommand: function (options) {
2013-09-12 03:42:15 +03:00
var self = this;
2013-09-12 03:42:15 +03:00
var existingCommand = CommandCollection.findCommand(options.command);
2013-09-12 03:42:15 +03:00
if (existingCommand) {
this._bindToCommandModel.call(this, existingCommand, options);
2013-09-12 03:42:15 +03:00
}
CommandCollection.bind('add sync', function (model) {
if (model.isSameCommand(options.command)) {
self._bindToCommandModel.call(self, model, options);
}
});
},
2013-09-12 03:42:15 +03:00
_bindToCommandModel: function bindToCommand(model, options) {
2013-09-12 03:42:15 +03:00
if (!model.isActive()) {
options.element.stopSpin();
return;
2013-09-12 03:42:15 +03:00
}
model.bind('change:state', function (model) {
if (!model.isActive()) {
options.element.stopSpin();
if (model.isComplete()) {
App.vent.trigger(App.Events.CommandComplete, { command: model, model: options.model });
}
}
});
options.element.startSpin();
}
};
};
return singleton();
});