arma-server-web-admin/public/js/app/views/mods/form.js

91 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-05-06 23:35:44 +00:00
define(function (require) {
'use strict';
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
Marionette = require('marionette'),
FormView = require('marionette-formview'),
2015-02-04 23:42:18 +00:00
ListItemView = require('app/views/mods/search/list_item'),
Ladda = require('ladda'),
2015-02-04 23:42:18 +00:00
Mods = require('app/collections/mods'),
2014-05-06 23:35:44 +00:00
tpl = require('text!tpl/mods/form.html');
2015-02-04 23:42:18 +00:00
return Marionette.CompositeView.extend({
2014-05-21 22:52:20 +00:00
events: {
'submit': 'beforeSubmit',
},
2015-02-04 23:42:18 +00:00
itemView: ListItemView,
itemViewContainer: "tbody",
2014-05-06 23:35:44 +00:00
template: _.template(tpl),
initialize: function (options) {
this.mods = options.mods;
2015-02-04 23:42:18 +00:00
this.collection = new Mods();
2014-05-06 23:35:44 +00:00
this.bind('ok', this.submit);
this.bind('shown', this.shown);
2015-04-07 06:12:42 +00:00
var self = this;
this.listenTo(this.mods, "change reset add remove", function () {
self.collection.trigger('reset');
});
},
itemViewOptions: function(options) {
options.set('mods', this.mods);
},
2014-05-21 22:52:20 +00:00
beforeSubmit: function(e) {
e.preventDefault();
this.submit();
},
shown: function (modal) {
var $okBtn = modal.$el.find('.btn.ok');
$okBtn.addClass('ladda-button').attr('data-style', 'expand-left');
this.laddaBtn = Ladda.create($okBtn.get(0));
2014-05-21 22:54:14 +00:00
this.$el.find('form .mod').focus();
2014-05-06 23:35:44 +00:00
},
submit: function (modal) {
var self = this;
2015-02-05 00:53:06 +00:00
var $form = this.$el.find('form');
2014-05-06 23:35:44 +00:00
2014-05-21 22:52:20 +00:00
if (modal) {
self.modal.preventClose();
}
2014-05-06 23:35:44 +00:00
$form.find('.form-group').removeClass('has-error');
$form.find('.help-block').text('');
this.laddaBtn.start();
2014-05-21 22:52:20 +00:00
self.modal.$el.find('.btn.cancel').addClass('disabled');
2015-02-04 23:42:18 +00:00
$.ajax({
url: '/api/mods/search',
type: 'POST',
data: {
query: $form.find('.query').val()
},
dataType: 'json',
success: function (data) {
self.laddaBtn.stop();
2015-02-04 23:42:18 +00:00
self.modal.$el.find('.btn.cancel').removeClass('disabled');
self.collection.set(data);
2014-05-06 23:35:44 +00:00
},
error: function () {
self.laddaBtn.stop();
2014-05-06 23:35:44 +00:00
$form.find('.form-group').addClass('has-error');
2015-02-04 23:42:18 +00:00
$form.find('.help-block').text('Problem searching, try again');
2014-05-21 22:52:20 +00:00
self.modal.$el.find('.btn.cancel').removeClass('disabled');
2014-05-06 23:35:44 +00:00
}
});
},
});
});