2013-06-22 06:24:24 +00:00
|
|
|
'use strict';
|
2013-09-11 06:33:47 +00:00
|
|
|
define(
|
|
|
|
[
|
2013-09-29 08:48:47 +00:00
|
|
|
'app',
|
2013-09-11 06:33:47 +00:00
|
|
|
'Commands/CommandModel',
|
|
|
|
'Commands/CommandCollection',
|
2013-09-12 00:42:15 +00:00
|
|
|
'underscore',
|
|
|
|
'jQuery/jquery.spin'
|
2013-09-29 08:48:47 +00:00
|
|
|
], function (App, CommandModel, CommandCollection, _) {
|
2013-05-11 23:39:32 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
var singleton = function () {
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
return {
|
2013-09-11 06:33:47 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
Execute: function (name, properties) {
|
2013-06-13 02:55:11 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
var attr = _.extend({name: name.toLocaleLowerCase()}, properties);
|
2013-09-11 06:33:47 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
var commandModel = new CommandModel(attr);
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
return commandModel.save().success(function () {
|
|
|
|
CommandCollection.add(commandModel);
|
|
|
|
});
|
|
|
|
},
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
bindToCommand: function (options) {
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
var self = this;
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
var existingCommand = CommandCollection.findCommand(options.command);
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
if (existingCommand) {
|
|
|
|
this._bindToCommandModel.call(this, existingCommand, options);
|
2013-09-12 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
CommandCollection.bind('add sync', function (model) {
|
|
|
|
if (model.isSameCommand(options.command)) {
|
|
|
|
self._bindToCommandModel.call(self, model, options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2013-09-12 00:42:15 +00:00
|
|
|
|
2013-09-29 08:48:47 +00:00
|
|
|
_bindToCommandModel: function bindToCommand(model, options) {
|
2013-09-12 00:42:15 +00:00
|
|
|
|
|
|
|
if (!model.isActive()) {
|
|
|
|
options.element.stopSpin();
|
2013-09-29 08:48:47 +00:00
|
|
|
return;
|
2013-09-12 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-29 08:48:47 +00: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();
|
|
|
|
}
|
|
|
|
};
|
2013-09-29 07:02:12 +00:00
|
|
|
};
|
2013-09-29 08:48:47 +00:00
|
|
|
|
|
|
|
return singleton();
|
2013-06-24 23:41:59 +00:00
|
|
|
});
|