// Registration Rate Collection
app.Collections.Admin.RegRates = Backbone.Collection.extend({
model: app.Models.Admin.RegRate,
- url: ajaxUrl+'&glm_action=regAdmin&collection=regRates'
-});
-
+ url: ajaxUrl+'&glm_action=regAdmin&collection=regRates',
+ comparator: function(rate1, rate2){
+ var r1 = parseInt( rate1.get( 'start_days' ) );
+ var r2 = parseInt( rate2.get( 'start_days' ) );
+ if ( r1 > r2 ) {
+ return -1;
+ } else if ( r1 == r2 ) {
+ return 0
+ } else {
+ return 1;
+ }
+ },
+});
+
+
// js/views/app.js
Backbone.emulateJSON = true;
Backbone.emulateHTTP = true;
initialize: function(){
this.listenTo( this.model, 'change', this.render );
-
+
// Use the reg rates sent as an attribute to the class to create the rates models
- if (this.model.attributes.reg_rate) {
+ if (this.model.attributes.reg_rate) {
this.model.setRates(this.model.attributes.reg_rate);
}
return this;
},
add: function(){
-
- console.log('add called');
+
+ console.log('add called in regClass.js');
// Get data from form
var cName = this.$('.class-name').val().trim();
alert('The Level Name is Required!');
return;
}
-
+
this.model.set({ name: cName, descr: cDescr });
// Get parent for id to send to backend for new class
var parentEvent = this.model.get( 'parent' );
-
+
// Try to save the registration class
var newID = false;
myself = this;
- this.model.save({
+ this.model.save({
wait: true,
- option: 'add',
- event: parentEvent.id,
- name: cName,
- descr: cDescr
+ option: 'add',
+ event: parentEvent.id,
+ name: cName,
+ descr: cDescr
}).success(function(data){
if (data > 0) {
console.log('AJAX Class Save Successful: ID = '+data);
});
console.log('New ID = '+newID);
-
- glmSubmitRequired -= 1;
+
+ glmSubmitRequired -= 1;
},
// Remove class here
var x = this.model.get('id');
- this.model.destroy({data: {
+ this.model.destroy({data: {
id: x,
option: 'delete'
}});
this.remove();
},
-
+
cancel: function(){
console.log('cancel called');
this.remove();
this.$('.class-edit').hide();
this.$('.class-delete').hide();
this.$('.class-name').focus();
- glmSubmitRequired += 1;
+ glmSubmitRequired += 1;
},
- update: function(){
+ update: function(){
console.log('update called');
var cName = this.$('.class-name').val().trim();
var cDescr = this.$('.class-descr').val().trim();
this.$('.class-update').hide();
this.$('.class-edit').show();
this.$('.class-delete').show();
- glmSubmitRequired -= 1;
+ glmSubmitRequired -= 1;
},
addRate: function(){
var reg_event = this.model.get('reg_event');
var reg_class = this.model.get('id');
- var regRate = new app.Models.Admin.RegRate({
- glmAction: 'regRate',
+ var regRate = new app.Models.Admin.RegRate({
+ glmAction: 'regRate',
parent: this.model,
reg_event: reg_event,
reg_class: reg_class
});
- var view = new app.Views.Admin.RegRate({
+ this.model.rates.add( regRate );
+ var view = new app.Views.Admin.RegRate({
model: regRate,
});
// Place rates inside the class-rate-container of the parent class
- var rateContainer = this.$('.class-rate-container');
+ var rateContainer = this.$('.class-rate-container');
rateContainer.append(view.render(true).el);
-
+
},
-
+
render: function(addFlag){
this.$el.html( this.template( this.model.toJSON() ) );
var rateView = this.model.rates.map(function(item){
return (new app.Views.Admin.RegRate({ model: item })).render().el;
});
- var rateContainer = this.$('.class-rate-container');
+ var rateContainer = this.$('.class-rate-container');
rateContainer.append(rateView);
-
- /*
- * If addFlag is true then we know the call to this is to add a class
- * and not to initialize existing rates. Note that addFLag may also
+
+ /*
+ * If addFlag is true then we know the call to this is to add a class
+ * and not to initialize existing rates. Note that addFLag may also
* be an object, which is why the explicit === below.
*/
if (addFlag === true) {
-
+
console.log('New level created');
this.$('.class-display-template').hide();
this.$('.class-edit-template').show();
this.$('.class-name').focus();
this.newClass = true;
glmSubmitRequired += 1;
-
+
}
return this;
},
this.listenTo( this.model, 'change', this.render );
return this;
},
-
+
events: {
'click .rate-edit': 'edit',
'click .rate-update': 'update',
},
getInputData: function() {
-
+
var input = {
name: this.$('.rate-name').val().trim(),
start_days: this.$('.rate-start-days').val().trim(),
};
return input;
},
-
+
currency: function (v) {
v = v.trim().replace ( /[^0-9.]/g, '' );
if (v == '') {
add: function(){
- console.log('add called');
-
+ console.log('add called in regRate.js');
+
// Get parent for id to send to backend for new rate
- var parentClass = this.model.get( 'parent' );
+ var parentClass = this.model.get( 'parent' );
var formData = this.getInputData();
- // Check for required data -- NEED TO MOVE THIS TO getInputData function
+ // Check for required data -- NEED TO MOVE THIS TO getInputData function
if (formData.name == '') {
alert('The Level Name is Required!');
return;
}
this.model.set(formData);
-
+
// Try to save the registration rate
var newID = false;
myself = this;
formData.wait = true;
formData.option = 'add';
formData.reg_event = parentClass.attributes.reg_event;
-
+
this.model.save(
formData
- ).success(function(data){
+ ).success(function(data){
if (data > 0) {
console.log('AJAX Rate Save Successful: ID = '+data);
myself.model.set({id: data});
myself.$('.rate-delete').show();
myself.$('.rate-add').hide();
myself.newRate = false;
+ myself.model.collection.sort();
} else {
alert('Unable to store rate data at this time.');
}
console.log(e);
alert('There was a problem communicating with the AJAX server.');
});
-
- glmSubmitRequired -= 1;
-
- calendar.fullCalendar('destroy');
-
+ glmSubmitRequired -= 1;
+
+ // calendar.fullCalendar('destroy');
+
+
},
delete: function() {
alert('Please complete your other edit first.');
return false;
}
-
+
if (!confirm('Are you sure you want to delete this?')) {
return false;
}
// Remove rate here
var x = this.model.get('id');
-
- this.model.destroy({data: {
+
+ this.model.destroy({data: {
id: x,
option: 'delete'
}});
-
+
this.remove();
-
+
},
-
+
edit: function() {
if (glmSubmitRequired) {
alert('Please complete your other edit first.');
this.$('.rate-delete').hide();
this.$('.rate-name').focus();
glmSubmitRequired += 1;
-
+
},
-
+
update: function() {
console.log('update called');
var datesError = this.checkDates(this.model, formData);
if (datesError != '') {
- alert(datesError);
+ alert(datesError);
return false;
}
-
+
formData.option = 'update';
if (!this.model.save(
formData
this.$('.rate-display-template').show();
this.$('.rate-edit-template').hide();
this.$('.rate-delete').show();
- glmSubmitRequired -= 1;
-
- calendar.fullCalendar('destroy');
+ glmSubmitRequired -= 1;
+
+ // calendar.fullCalendar('destroy');
},
-
+
render: function(addFlag){
- this.$el.html( this.template( this.model.toJSON() ) );
-
- /*
- * If addFlag is true then we know the call to this is to add a rate
- * and not to initialize existing rates. Note that addFLag may also
+ this.$el.html( this.template( this.model.toJSON() ) );
+
+ /*
+ * If addFlag is true then we know the call to this is to add a rate
+ * and not to initialize existing rates. Note that addFLag may also
* be an object, which is why the explicit === below.
*/
if (addFlag === true) {
-
+
// Setup to edit this for the first time
console.log('New rate created');
this.$('.rate-display-template').hide();
this.$('.rate-name').focus();
this.newRate = true;
glmSubmitRequired += 1;
-
+
}
return this;
},
-
+
checkDates: function(model, current) {
-
+
var datesError = '';
-
+
var cur = model.cid; // ID of current rate
var start = parseInt(current.start_days);
var end = parseInt(current.end_days);
-
+
if (start < end) {
datesError += 'Start days must be greater than end days. ';
} else {
-
+
// For each rate
Object.entries(model.collection.models).forEach(([key, value]) => {
-
+
// Don't compare with self
if (cur != value.cid) {
-
+
// If the sumbitted days overlap this rate
if ((end - value.attributes.start_days) * (value.attributes.end_days - start) >= 0) {
datesError += 'Days for this rate overlap another rate. Each rate must cover a separate days range';
}
-
+
}
-
+
});
}
-
+
return datesError;
}
-
+
});
initialize: function(){
this.listenTo( this.model, 'change', this.render );
-
+
// Use the reg rates sent as an attribute to the class to create the rates models
- if (this.model.attributes.reg_rate) {
+ if (this.model.attributes.reg_rate) {
this.model.setRates(this.model.attributes.reg_rate);
}
return this;
},
add: function(){
-
- console.log('add called');
+
+ console.log('add called in regClass.js');
// Get data from form
var cName = this.$('.class-name').val().trim();
alert('The Level Name is Required!');
return;
}
-
+
this.model.set({ name: cName, descr: cDescr });
// Get parent for id to send to backend for new class
var parentEvent = this.model.get( 'parent' );
-
+
// Try to save the registration class
var newID = false;
myself = this;
- this.model.save({
+ this.model.save({
wait: true,
- option: 'add',
- event: parentEvent.id,
- name: cName,
- descr: cDescr
+ option: 'add',
+ event: parentEvent.id,
+ name: cName,
+ descr: cDescr
}).success(function(data){
if (data > 0) {
console.log('AJAX Class Save Successful: ID = '+data);
});
console.log('New ID = '+newID);
-
- glmSubmitRequired -= 1;
+
+ glmSubmitRequired -= 1;
},
// Remove class here
var x = this.model.get('id');
- this.model.destroy({data: {
+ this.model.destroy({data: {
id: x,
option: 'delete'
}});
this.remove();
},
-
+
cancel: function(){
console.log('cancel called');
this.remove();
this.$('.class-edit').hide();
this.$('.class-delete').hide();
this.$('.class-name').focus();
- glmSubmitRequired += 1;
+ glmSubmitRequired += 1;
},
- update: function(){
+ update: function(){
console.log('update called');
var cName = this.$('.class-name').val().trim();
var cDescr = this.$('.class-descr').val().trim();
this.$('.class-update').hide();
this.$('.class-edit').show();
this.$('.class-delete').show();
- glmSubmitRequired -= 1;
+ glmSubmitRequired -= 1;
},
addRate: function(){
var reg_event = this.model.get('reg_event');
var reg_class = this.model.get('id');
- var regRate = new app.Models.Admin.RegRate({
- glmAction: 'regRate',
+ var regRate = new app.Models.Admin.RegRate({
+ glmAction: 'regRate',
parent: this.model,
reg_event: reg_event,
reg_class: reg_class
});
- var view = new app.Views.Admin.RegRate({
+ this.model.rates.add( regRate );
+ var view = new app.Views.Admin.RegRate({
model: regRate,
});
// Place rates inside the class-rate-container of the parent class
- var rateContainer = this.$('.class-rate-container');
+ var rateContainer = this.$('.class-rate-container');
rateContainer.append(view.render(true).el);
-
+
},
-
+
render: function(addFlag){
this.$el.html( this.template( this.model.toJSON() ) );
var rateView = this.model.rates.map(function(item){
return (new app.Views.Admin.RegRate({ model: item })).render().el;
});
- var rateContainer = this.$('.class-rate-container');
+ var rateContainer = this.$('.class-rate-container');
rateContainer.append(rateView);
-
- /*
- * If addFlag is true then we know the call to this is to add a class
- * and not to initialize existing rates. Note that addFLag may also
+
+ /*
+ * If addFlag is true then we know the call to this is to add a class
+ * and not to initialize existing rates. Note that addFLag may also
* be an object, which is why the explicit === below.
*/
if (addFlag === true) {
-
+
console.log('New level created');
this.$('.class-display-template').hide();
this.$('.class-edit-template').show();
this.$('.class-name').focus();
this.newClass = true;
glmSubmitRequired += 1;
-
+
}
return this;
},
this.listenTo( this.model, 'change', this.render );
return this;
},
-
+
events: {
'click .rate-edit': 'edit',
'click .rate-update': 'update',
},
getInputData: function() {
-
+
var input = {
name: this.$('.rate-name').val().trim(),
start_days: this.$('.rate-start-days').val().trim(),
};
return input;
},
-
+
currency: function (v) {
v = v.trim().replace ( /[^0-9.]/g, '' );
if (v == '') {
add: function(){
- console.log('add called');
-
+ console.log('add called in regRate.js');
+
// Get parent for id to send to backend for new rate
- var parentClass = this.model.get( 'parent' );
+ var parentClass = this.model.get( 'parent' );
var formData = this.getInputData();
- // Check for required data -- NEED TO MOVE THIS TO getInputData function
+ // Check for required data -- NEED TO MOVE THIS TO getInputData function
if (formData.name == '') {
alert('The Level Name is Required!');
return;
}
this.model.set(formData);
-
+
// Try to save the registration rate
var newID = false;
myself = this;
formData.wait = true;
formData.option = 'add';
formData.reg_event = parentClass.attributes.reg_event;
-
+
this.model.save(
formData
- ).success(function(data){
+ ).success(function(data){
if (data > 0) {
console.log('AJAX Rate Save Successful: ID = '+data);
myself.model.set({id: data});
myself.$('.rate-delete').show();
myself.$('.rate-add').hide();
myself.newRate = false;
+ myself.model.collection.sort();
} else {
alert('Unable to store rate data at this time.');
}
console.log(e);
alert('There was a problem communicating with the AJAX server.');
});
-
- glmSubmitRequired -= 1;
-
- calendar.fullCalendar('destroy');
-
+ glmSubmitRequired -= 1;
+
+ // calendar.fullCalendar('destroy');
+
+
},
delete: function() {
alert('Please complete your other edit first.');
return false;
}
-
+
if (!confirm('Are you sure you want to delete this?')) {
return false;
}
// Remove rate here
var x = this.model.get('id');
-
- this.model.destroy({data: {
+
+ this.model.destroy({data: {
id: x,
option: 'delete'
}});
-
+
this.remove();
-
+
},
-
+
edit: function() {
if (glmSubmitRequired) {
alert('Please complete your other edit first.');
this.$('.rate-delete').hide();
this.$('.rate-name').focus();
glmSubmitRequired += 1;
-
+
},
-
+
update: function() {
console.log('update called');
var datesError = this.checkDates(this.model, formData);
if (datesError != '') {
- alert(datesError);
+ alert(datesError);
return false;
}
-
+
formData.option = 'update';
if (!this.model.save(
formData
this.$('.rate-display-template').show();
this.$('.rate-edit-template').hide();
this.$('.rate-delete').show();
- glmSubmitRequired -= 1;
-
- calendar.fullCalendar('destroy');
+ glmSubmitRequired -= 1;
+
+ // calendar.fullCalendar('destroy');
},
-
+
render: function(addFlag){
- this.$el.html( this.template( this.model.toJSON() ) );
-
- /*
- * If addFlag is true then we know the call to this is to add a rate
- * and not to initialize existing rates. Note that addFLag may also
+ this.$el.html( this.template( this.model.toJSON() ) );
+
+ /*
+ * If addFlag is true then we know the call to this is to add a rate
+ * and not to initialize existing rates. Note that addFLag may also
* be an object, which is why the explicit === below.
*/
if (addFlag === true) {
-
+
// Setup to edit this for the first time
console.log('New rate created');
this.$('.rate-display-template').hide();
this.$('.rate-name').focus();
this.newRate = true;
glmSubmitRequired += 1;
-
+
}
return this;
},
-
+
checkDates: function(model, current) {
-
+
var datesError = '';
-
+
var cur = model.cid; // ID of current rate
var start = parseInt(current.start_days);
var end = parseInt(current.end_days);
-
+
if (start < end) {
datesError += 'Start days must be greater than end days. ';
} else {
-
+
// For each rate
Object.entries(model.collection.models).forEach(([key, value]) => {
-
+
// Don't compare with self
if (cur != value.cid) {
-
+
// If the sumbitted days overlap this rate
if ((end - value.attributes.start_days) * (value.attributes.end_days - start) >= 0) {
datesError += 'Days for this rate overlap another rate. Each rate must cover a separate days range';
}
-
+
}
-
+
});
}
-
+
return datesError;
}
-
+
});