2013-07-22 04:23:17 +02:00
|
|
|
'use strict';
|
|
|
|
define(['Commands/CommandController', 'Shared/Messenger'],
|
|
|
|
function(CommandController, Messenger) {
|
|
|
|
return {
|
|
|
|
ExecuteCommand: function (options) {
|
|
|
|
options.iconClass = this._getIconClass(options.element);
|
|
|
|
|
2013-07-22 04:52:53 +02:00
|
|
|
this._showStartMessage(options);
|
2013-07-22 04:23:17 +02:00
|
|
|
this._setSpinnerOnElement(options);
|
|
|
|
|
|
|
|
var promise = CommandController.Execute(options.command, options.properties);
|
|
|
|
this._handlePromise(promise, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
SaveModel: function (options) {
|
|
|
|
options.iconClass = this._getIconClass(options.element);
|
|
|
|
|
2013-07-22 04:52:53 +02:00
|
|
|
this._showStartMessage(options);
|
2013-07-22 04:23:17 +02:00
|
|
|
this._setSpinnerOnElement(options);
|
|
|
|
|
2013-07-22 04:52:53 +02:00
|
|
|
var promise = options.context.model.save();
|
2013-07-22 04:23:17 +02:00
|
|
|
this._handlePromise(promise, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
_handlePromise: function (promise, options) {
|
|
|
|
promise.done(function () {
|
|
|
|
if (options.successMessage) {
|
|
|
|
Messenger.show({
|
|
|
|
message: options.successMessage
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-08 09:38:14 +02:00
|
|
|
if (options.onSuccess) {
|
|
|
|
options.onSuccess.call(options.context);
|
2013-07-22 04:23:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.fail(function (ajaxOptions) {
|
|
|
|
if (ajaxOptions.readyState === 0 || ajaxOptions.status === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.failMessage) {
|
|
|
|
Messenger.show({
|
|
|
|
message: options.failMessage,
|
|
|
|
type : 'error'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-08 09:38:14 +02:00
|
|
|
if (options.onError) {
|
|
|
|
options.onError.call(options.context);
|
2013-07-22 04:23:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.always(function () {
|
|
|
|
|
|
|
|
if (options.leaveIcon) {
|
|
|
|
options.element.removeClass('icon-spin');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
options.element.addClass(options.iconClass);
|
|
|
|
options.element.removeClass('icon-nd-spinner');
|
|
|
|
}
|
|
|
|
|
2013-08-08 09:38:14 +02:00
|
|
|
if (options.always) {
|
|
|
|
options.always.call(options.context);
|
2013-07-22 04:23:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_getIconClass: function(element) {
|
|
|
|
return element.attr('class').match(/(?:^|\s)icon\-.+?(?:$|\s)/)[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
_setSpinnerOnElement: function (options) {
|
|
|
|
if (options.leaveIcon) {
|
|
|
|
options.element.addClass('icon-spin');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
options.element.removeClass(options.iconClass);
|
|
|
|
options.element.addClass('icon-nd-spinner');
|
|
|
|
}
|
2013-07-22 04:52:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
_showStartMessage: function (options) {
|
|
|
|
if (options.startMessage) {
|
|
|
|
Messenger.show({
|
|
|
|
message: options.startMessage
|
|
|
|
});
|
|
|
|
}
|
2013-07-22 04:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|