From 0cac267e67fc5e06c72f7807d100d46903ae4d83 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 6 Nov 2017 16:09:50 -0500 Subject: [PATCH] Updated front end for new regTime model,collection,view. Adding model collection view for the regTime. Setup to add registrant from the regTime view. --- js/frontRegApp.js | 75 +++++++++++++-------- js/models/front/regEvent.js | 18 +++-- js/views/front/regClass.js | 23 ++++--- js/views/front/regTime.js | 34 ++++++---- models/front/registrations/registration.php | 2 +- views/front/registrations/registration.html | 4 +- 6 files changed, 99 insertions(+), 57 deletions(-) diff --git a/js/frontRegApp.js b/js/frontRegApp.js index 6402e9f..4e40b59 100644 --- a/js/frontRegApp.js +++ b/js/frontRegApp.js @@ -160,11 +160,21 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ setClassRegistrants: function( registrants ){ for ( var i = 0; i < registrants.length; i++ ) { var foundClass = this.classes.findWhere({ id: registrants[i].class_id }); - console.log( foundClass ); if ( foundClass != undefined ) { - foundClass.registrants.create( registrants[i] ); - var newReg = foundClass.registrants.pop(); - foundClass.set({selectedTime: newReg.get( 'reg_time' ) }); + var foundRegTime = _.where(app.Times, { id: registrants[i].reg_time }); + // Now add the time if needed + var hasRegTime = _.findWhere(foundClass, { reg_time: foundRegTime.id }); + if ( !hasRegTime ) { + foundClass.regTimes.create( foundRegTime[0] ); + hasRegTime = foundClass.regTimes.findWhere({ id: foundRegTime[0].id }); + } + // Add the registrant + var nRegTime = foundClass.regTimes.findWhere({id: hasRegTime.get('id')}); + // console.log( nRegTime ); + nRegTime.registrants.create( registrants[i] ); + + // var newReg = foundClass.registrants.pop(); + // foundClass.set({selectedTime: newReg.get( 'reg_time' ) }); } } }, @@ -467,20 +477,25 @@ app.Views.Front.RegClass = Backbone.View.extend({ }, updateTimeSelection: function(e){ - // console.log(e); - // if ( this.newRegAccount ) { - // this.newRegAccount.destroy(); - // this.newRegAccountView.remove(); - // } // Check to see if the time is already in the collection // If it is then do nothing - var findRegTime = this.model.regTimes.get( e.currentTarget.value ); + var findRegTime = this.model.regTimes.findWhere({ id: e.currentTarget.value }); if ( !findRegTime ) { this.model.set({ selectedTime: parseInt( e.currentTarget.value ) }); // Add the RegTime model/view into this view - var foundRegTime = app.times.get( e.currentTarget.value ); - foundRegTime.set({parent: this.model}); - this.model.regTimes.create( foundRegTime ); + // console.log( e.currentTarget.value ); + console.log( app.Times ); + var targetTime = null; + for ( var i = 0; i < app.Times.length; i++ ) { + if ( app.Times[i].id == e.currentTarget.value ) { + targetTime = app.Times[i]; + } + } + if ( targetTime ) { + var selectedTime = new app.Models.Front.RegTime( targetTime ); + selectedTime.set({parent: this.model}); + this.model.regTimes.create( selectedTime ); + } } }, @@ -941,7 +956,7 @@ app.Views.Front.RegRequestRegistrant = Backbone.View.extend({ }); -// js/views/regClass.js +// js/views/front/regTime.js var newId = 0; app.Views.Front.RegTime = Backbone.View.extend({ tagName: 'div', @@ -951,6 +966,10 @@ app.Views.Front.RegTime = Backbone.View.extend({ template: _.template( jQuery('#regTime-template').html() ), initialize: function(){ + this.listenTo( this.model.registrants, 'add', this.addOne ); + this.listenTo( this.model.registrants, 'create', this.addOne ); + this.listenTo( this.model.registrants, 'remove', this.deleteOne ); + this.listenTo( this.model, 'change', this.render ); return this; }, @@ -995,22 +1014,17 @@ app.Views.Front.RegTime = Backbone.View.extend({ var state = this.$('.add_reg_state').val().trim(); var zip = this.$('.add_reg_zip').val().trim(); var country = this.$('.add_reg_country').val().trim(); - var sTime = this.model.get( 'selectedTime' ); + var sTime = this.model.get( 'rate_id' ); if ( this.$('#add_reg-select-time').length ) { var times = this.model.get( 'times' ); - var rateId = null; - // Setup the needed reg_rate - _.each( times, function( time ){ - if ( time.id == sTime ) { - rateId = time.rate_id; - } - } ); } else { var times = this.model.get( 'times' ); - var rateId = null; - _.each( times, function( time ){ - rateId = time.rate_id; - } ); + } + var rateId = this.model.get( 'rate_id' ); + if ( !rateId ) { + console.log( 'No rate id!' ); + console.log( sTime ); + return; } if ( fname === '' || lname === '' ) { alert( 'First and Last name required!' ); @@ -1031,7 +1045,7 @@ app.Views.Front.RegTime = Backbone.View.extend({ reg_request: regRequest.id, reg_time: this.model.get( 'id' ), reg_event: this.model.get( 'reg_event' ), // Todo: use correct reg_event - reg_class: this.model.get( 'id' ), // Todo: use correct reg_class + reg_class: this.model.get( 'parent' ).id, // Todo: use correct reg_class event_name: regEvent.get( 'event_name' ), reg_rate: rateId, email: email, @@ -1064,7 +1078,14 @@ app.Views.Front.RegTime = Backbone.View.extend({ } }, + addOne: function( item ){ + this.model.set({selectedTime: item.get('reg_time') }) + this.render(); + }, + deleteOne: function( item ){ + this.render(); + }, }); app.Views.Front.RegistrantForm = Backbone.View.extend({ diff --git a/js/models/front/regEvent.js b/js/models/front/regEvent.js index 2d70233..5dc68c5 100644 --- a/js/models/front/regEvent.js +++ b/js/models/front/regEvent.js @@ -29,11 +29,21 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ setClassRegistrants: function( registrants ){ for ( var i = 0; i < registrants.length; i++ ) { var foundClass = this.classes.findWhere({ id: registrants[i].class_id }); - console.log( foundClass ); if ( foundClass != undefined ) { - foundClass.registrants.create( registrants[i] ); - var newReg = foundClass.registrants.pop(); - foundClass.set({selectedTime: newReg.get( 'reg_time' ) }); + var foundRegTime = _.where(app.Times, { id: registrants[i].reg_time }); + // Now add the time if needed + var hasRegTime = _.findWhere(foundClass, { reg_time: foundRegTime.id }); + if ( !hasRegTime ) { + foundClass.regTimes.create( foundRegTime[0] ); + hasRegTime = foundClass.regTimes.findWhere({ id: foundRegTime[0].id }); + } + // Add the registrant + var nRegTime = foundClass.regTimes.findWhere({id: hasRegTime.get('id')}); + // console.log( nRegTime ); + nRegTime.registrants.create( registrants[i] ); + + // var newReg = foundClass.registrants.pop(); + // foundClass.set({selectedTime: newReg.get( 'reg_time' ) }); } } }, diff --git a/js/views/front/regClass.js b/js/views/front/regClass.js index d15d28d..34f1ab9 100644 --- a/js/views/front/regClass.js +++ b/js/views/front/regClass.js @@ -28,20 +28,25 @@ app.Views.Front.RegClass = Backbone.View.extend({ }, updateTimeSelection: function(e){ - // console.log(e); - // if ( this.newRegAccount ) { - // this.newRegAccount.destroy(); - // this.newRegAccountView.remove(); - // } // Check to see if the time is already in the collection // If it is then do nothing - var findRegTime = this.model.regTimes.get( e.currentTarget.value ); + var findRegTime = this.model.regTimes.findWhere({ id: e.currentTarget.value }); if ( !findRegTime ) { this.model.set({ selectedTime: parseInt( e.currentTarget.value ) }); // Add the RegTime model/view into this view - var foundRegTime = app.times.get( e.currentTarget.value ); - foundRegTime.set({parent: this.model}); - this.model.regTimes.create( foundRegTime ); + // console.log( e.currentTarget.value ); + console.log( app.Times ); + var targetTime = null; + for ( var i = 0; i < app.Times.length; i++ ) { + if ( app.Times[i].id == e.currentTarget.value ) { + targetTime = app.Times[i]; + } + } + if ( targetTime ) { + var selectedTime = new app.Models.Front.RegTime( targetTime ); + selectedTime.set({parent: this.model}); + this.model.regTimes.create( selectedTime ); + } } }, diff --git a/js/views/front/regTime.js b/js/views/front/regTime.js index 9d62571..4b12fb2 100644 --- a/js/views/front/regTime.js +++ b/js/views/front/regTime.js @@ -1,4 +1,4 @@ -// js/views/regClass.js +// js/views/front/regTime.js var newId = 0; app.Views.Front.RegTime = Backbone.View.extend({ tagName: 'div', @@ -8,6 +8,10 @@ app.Views.Front.RegTime = Backbone.View.extend({ template: _.template( jQuery('#regTime-template').html() ), initialize: function(){ + this.listenTo( this.model.registrants, 'add', this.addOne ); + this.listenTo( this.model.registrants, 'create', this.addOne ); + this.listenTo( this.model.registrants, 'remove', this.deleteOne ); + this.listenTo( this.model, 'change', this.render ); return this; }, @@ -52,22 +56,17 @@ app.Views.Front.RegTime = Backbone.View.extend({ var state = this.$('.add_reg_state').val().trim(); var zip = this.$('.add_reg_zip').val().trim(); var country = this.$('.add_reg_country').val().trim(); - var sTime = this.model.get( 'selectedTime' ); + var sTime = this.model.get( 'rate_id' ); if ( this.$('#add_reg-select-time').length ) { var times = this.model.get( 'times' ); - var rateId = null; - // Setup the needed reg_rate - _.each( times, function( time ){ - if ( time.id == sTime ) { - rateId = time.rate_id; - } - } ); } else { var times = this.model.get( 'times' ); - var rateId = null; - _.each( times, function( time ){ - rateId = time.rate_id; - } ); + } + var rateId = this.model.get( 'rate_id' ); + if ( !rateId ) { + console.log( 'No rate id!' ); + console.log( sTime ); + return; } if ( fname === '' || lname === '' ) { alert( 'First and Last name required!' ); @@ -88,7 +87,7 @@ app.Views.Front.RegTime = Backbone.View.extend({ reg_request: regRequest.id, reg_time: this.model.get( 'id' ), reg_event: this.model.get( 'reg_event' ), // Todo: use correct reg_event - reg_class: this.model.get( 'id' ), // Todo: use correct reg_class + reg_class: this.model.get( 'parent' ).id, // Todo: use correct reg_class event_name: regEvent.get( 'event_name' ), reg_rate: rateId, email: email, @@ -121,5 +120,12 @@ app.Views.Front.RegTime = Backbone.View.extend({ } }, + addOne: function( item ){ + this.model.set({selectedTime: item.get('reg_time') }) + this.render(); + }, + deleteOne: function( item ){ + this.render(); + }, }); diff --git a/models/front/registrations/registration.php b/models/front/registrations/registration.php index 3ff63b7..751787d 100644 --- a/models/front/registrations/registration.php +++ b/models/front/registrations/registration.php @@ -322,7 +322,7 @@ } // - End loop through events. } - // echo '
$registrants: ' . print_r( $registrants, true ) . '
'; + echo '
$registrants: ' . print_r( $registrants, true ) . '
'; // Compile template data $templateData = array( diff --git a/views/front/registrations/registration.html b/views/front/registrations/registration.html index f8f6626..353d7e3 100644 --- a/views/front/registrations/registration.html +++ b/views/front/registrations/registration.html @@ -203,7 +203,7 @@ {/literal}{if $regEvent.time_specific.value}
- Selected Time: {literal}<%= reg_time_text %>{/literal} +
{/if}{literal} @@ -236,6 +236,7 @@ var app = { Models: { Front: {}, Admin: {} }, Collections: { Front: {}, Admin: {} }, Views: { Front: {}, Admin: {} }, + Times: {$regTimesJSON}, isValidEmail: function( email ){ var regex = {literal}/^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;{/literal} return regex.test(email); @@ -266,6 +267,5 @@ jQuery(function($){ regEvent.setClasses( {$regClassesJSON} ); regEvent.setClassRegistrants( {$regJSON} ); var glmApp = new app.Views.Front.App(); - app.times = new app.Collections.Front.RegTimes( {$regTimesJSON} ); }); -- 2.17.1