From: Steve Sutton Date: Thu, 26 Oct 2017 20:36:52 +0000 (-0400) Subject: Add comarator to rate collection X-Git-Tag: v1.0.0^2~314 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=abd2113e7db61b8b1c4e87ff3fa5127613b4b070;p=WP-Plugins%2Fglm-member-db-registrations.git Add comarator to rate collection This should keep the rates sorted. Not working for me yet. They only get sorted once the rates are added and you refresh the page. --- diff --git a/js/adminRegApp.js b/js/adminRegApp.js index 302a213..39f7f88 100644 --- a/js/adminRegApp.js +++ b/js/adminRegApp.js @@ -90,9 +90,21 @@ app.Collections.Admin.RegClasses = Backbone.Collection.extend({ // 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; @@ -128,9 +140,9 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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; @@ -146,8 +158,8 @@ app.Views.Admin.RegClass = Backbone.View.extend({ }, add: function(){ - - console.log('add called'); + + console.log('add called in regClass.js'); // Get data from form var cName = this.$('.class-name').val().trim(); @@ -158,21 +170,21 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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); @@ -193,8 +205,8 @@ app.Views.Admin.RegClass = Backbone.View.extend({ }); console.log('New ID = '+newID); - - glmSubmitRequired -= 1; + + glmSubmitRequired -= 1; }, @@ -215,14 +227,14 @@ app.Views.Admin.RegClass = Backbone.View.extend({ // 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(); @@ -240,10 +252,10 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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(); @@ -257,7 +269,7 @@ app.Views.Admin.RegClass = Backbone.View.extend({ this.$('.class-update').hide(); this.$('.class-edit').show(); this.$('.class-delete').show(); - glmSubmitRequired -= 1; + glmSubmitRequired -= 1; }, addRate: function(){ @@ -272,39 +284,40 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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(); @@ -315,7 +328,7 @@ app.Views.Admin.RegClass = Backbone.View.extend({ this.$('.class-name').focus(); this.newClass = true; glmSubmitRequired += 1; - + } return this; }, @@ -392,7 +405,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ this.listenTo( this.model, 'change', this.render ); return this; }, - + events: { 'click .rate-edit': 'edit', 'click .rate-update': 'update', @@ -402,7 +415,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ }, getInputData: function() { - + var input = { name: this.$('.rate-name').val().trim(), start_days: this.$('.rate-start-days').val().trim(), @@ -413,7 +426,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ }; return input; }, - + currency: function (v) { v = v.trim().replace ( /[^0-9.]/g, '' ); if (v == '') { @@ -425,31 +438,31 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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}); @@ -459,6 +472,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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.'); } @@ -466,12 +480,12 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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() { @@ -481,23 +495,23 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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.'); @@ -510,9 +524,9 @@ app.Views.Admin.RegRate = Backbone.View.extend({ this.$('.rate-delete').hide(); this.$('.rate-name').focus(); glmSubmitRequired += 1; - + }, - + update: function() { console.log('update called'); @@ -520,10 +534,10 @@ app.Views.Admin.RegRate = Backbone.View.extend({ var datesError = this.checkDates(this.model, formData); if (datesError != '') { - alert(datesError); + alert(datesError); return false; } - + formData.option = 'update'; if (!this.model.save( formData @@ -535,23 +549,23 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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(); @@ -562,41 +576,41 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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; } - + }); diff --git a/js/collections/admin/regRates.js b/js/collections/admin/regRates.js index dfdbc75..52fc1ec 100644 --- a/js/collections/admin/regRates.js +++ b/js/collections/admin/regRates.js @@ -3,6 +3,17 @@ // Registration Rate Collection app.Collections.Admin.RegRates = Backbone.Collection.extend({ model: app.Models.Admin.RegRate, - url: ajaxUrl+'&glm_action=regAdmin&collection=regRates' -}); - \ No newline at end of file + 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; + } + }, +}); + diff --git a/js/views/admin/regClass.js b/js/views/admin/regClass.js index f1ca5f3..ab9b489 100644 --- a/js/views/admin/regClass.js +++ b/js/views/admin/regClass.js @@ -9,9 +9,9 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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; @@ -27,8 +27,8 @@ app.Views.Admin.RegClass = Backbone.View.extend({ }, add: function(){ - - console.log('add called'); + + console.log('add called in regClass.js'); // Get data from form var cName = this.$('.class-name').val().trim(); @@ -39,21 +39,21 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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); @@ -74,8 +74,8 @@ app.Views.Admin.RegClass = Backbone.View.extend({ }); console.log('New ID = '+newID); - - glmSubmitRequired -= 1; + + glmSubmitRequired -= 1; }, @@ -96,14 +96,14 @@ app.Views.Admin.RegClass = Backbone.View.extend({ // 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(); @@ -121,10 +121,10 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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(); @@ -138,7 +138,7 @@ app.Views.Admin.RegClass = Backbone.View.extend({ this.$('.class-update').hide(); this.$('.class-edit').show(); this.$('.class-delete').show(); - glmSubmitRequired -= 1; + glmSubmitRequired -= 1; }, addRate: function(){ @@ -153,39 +153,40 @@ app.Views.Admin.RegClass = Backbone.View.extend({ 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(); @@ -196,7 +197,7 @@ app.Views.Admin.RegClass = Backbone.View.extend({ this.$('.class-name').focus(); this.newClass = true; glmSubmitRequired += 1; - + } return this; }, diff --git a/js/views/admin/regRate.js b/js/views/admin/regRate.js index a250f00..c3b6d48 100644 --- a/js/views/admin/regRate.js +++ b/js/views/admin/regRate.js @@ -13,7 +13,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ this.listenTo( this.model, 'change', this.render ); return this; }, - + events: { 'click .rate-edit': 'edit', 'click .rate-update': 'update', @@ -23,7 +23,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ }, getInputData: function() { - + var input = { name: this.$('.rate-name').val().trim(), start_days: this.$('.rate-start-days').val().trim(), @@ -34,7 +34,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ }; return input; }, - + currency: function (v) { v = v.trim().replace ( /[^0-9.]/g, '' ); if (v == '') { @@ -46,31 +46,31 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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}); @@ -80,6 +80,7 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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.'); } @@ -87,12 +88,12 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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() { @@ -102,23 +103,23 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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.'); @@ -131,9 +132,9 @@ app.Views.Admin.RegRate = Backbone.View.extend({ this.$('.rate-delete').hide(); this.$('.rate-name').focus(); glmSubmitRequired += 1; - + }, - + update: function() { console.log('update called'); @@ -141,10 +142,10 @@ app.Views.Admin.RegRate = Backbone.View.extend({ var datesError = this.checkDates(this.model, formData); if (datesError != '') { - alert(datesError); + alert(datesError); return false; } - + formData.option = 'update'; if (!this.model.save( formData @@ -156,23 +157,23 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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(); @@ -183,41 +184,41 @@ app.Views.Admin.RegRate = Backbone.View.extend({ 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; } - + });