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