From b2a3362da3c3fa8fc69cdf2266f31ac18febb05b Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 15 Dec 2017 16:32:56 -0500 Subject: [PATCH] Updating things for the custom fields. Trying to get the models and views to have better garbage collection. Need to remove the model and view that is used for the add forms after it creates the new model in the collection. --- js/frontRegApp.js | 91 +++++++-------------- js/models/front/regEvent.js | 10 ++- js/views/front/regClass.js | 21 ++--- js/views/front/regRequestRegistrant.js | 23 +----- js/views/front/regTime.js | 15 ++-- js/views/front/registrantForm.js | 24 +----- views/front/registrations/registration.html | 31 ++++++- 7 files changed, 86 insertions(+), 129 deletions(-) diff --git a/js/frontRegApp.js b/js/frontRegApp.js index 39ca6b7..1c63d32 100644 --- a/js/frontRegApp.js +++ b/js/frontRegApp.js @@ -135,12 +135,13 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ initialize: function(){ // for setting up classes - this.classes = new app.Collections.Front.RegClasses(); + this.classes = new app.Collections.Front.RegClasses( [] ); }, // setClasses // To setup the classes collection setClasses: function( regClasses ){ + console.log( regClasses ); this.classes.reset( regClasses ); }, @@ -160,7 +161,8 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ // Using the registrants reg_time lookup the regTime // var foundRegTime = foundClass.regTimes.findWhere({id: registrants[i].reg_time}); var foundRegTime = _.findWhere(foundClass.get('times'), { id: registrants[i].reg_time }); - // console.log( foundRegTime ); + console.log( 'find reg time' ); + console.log( foundRegTime ); if ( foundRegTime ) { // Set the reg_event for this reg_time to the class we found for registrant foundRegTime.reg_event = foundClass.get('reg_event'); @@ -176,6 +178,8 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ } // Add the registrant to the regTime. var nRegTime = foundClass.regTimes.findWhere({id: hasRegTime.get('id')}); + console.log( 'nRegTime' ); + console.log(nRegTime); nRegTime.registrants.create( registrants[i] ); } @@ -506,11 +510,14 @@ app.Views.Front.RegClass = Backbone.View.extend({ // Add times into regTimes collection var classTimes = this.model.get('times'); + var timeArs = []; for ( var key in classTimes ) { var newTime = new app.Models.Front.RegTime( classTimes[key] ); newTime.set({reg_event: this.model.get('reg_event'), parent: this.model}); - this.model.regTimes.create( newTime ); + timeArs.push( newTime ); + // this.model.regTimes.create( newTime ); } + this.model.regTimes.reset( timeArs ); // console.log( classTimes ); @@ -559,22 +566,17 @@ app.Views.Front.RegClass = Backbone.View.extend({ // this.model.set({ reg_count: this.model.registrants.length }); this.$el.html( this.template( this.model.toJSON() ) ); // Go through the RegTimes Collection and render those - // var view = this.model. - // this.model.regTimes.sort(); // var classId = this.model.id; - var view = this.model.regTimes.map(function(item){ - // item.parent = classId; - return (new app.Views.Front.RegTime({ model: item })).render().el; - }); var timeContainer = this.$('.reg-class-times'); - timeContainer.append( view ); if ( loginAccount === '' ) { this.$('.glm-add-account').hide(); } if ( !app.timeSpecific ) { // this.$('.glm-add-new-account').show(); - this.updateTimeSelection(); + // this.updateTimeSelection(); + console.log( this.model.regTimes ); + } else { var sTime = this.model.get( 'selectedTime' ); if ( !sTime ) { @@ -583,7 +585,10 @@ app.Views.Front.RegClass = Backbone.View.extend({ this.$('.glm-add-new-account').show(); } } - this.onRenderComplete(); + var view = this.model.regTimes.map(function(item){ + return (new app.Views.Front.RegTime({ model: item })).render().el; + }); + timeContainer.append( view ); return this; }, @@ -880,7 +885,7 @@ app.Views.Front.RegRequestRegistrant = Backbone.View.extend({ this.$el.html( this.template( this.model.toJSON() ) ); this.$emailInput = this.$('.editEmail'); this.$('.reg-edit-form').hide(); - this.setCustomFieldsForm(); + app.setCustomFieldsForm(this.model.get('reg_event'), '.attendee-cf-' + this.model.id); return this; }, @@ -896,27 +901,6 @@ app.Views.Front.RegRequestRegistrant = Backbone.View.extend({ this.remove(); }, - setCustomFieldsForm: function(){ - var formData = { - 'action': 'glm_members_admin_ajax', - 'glm_action': 'customFieldsFront', - 'option': 'displayForm', - 'fid': 'glm_reg_customfields_reg_event_attendee_' + this.model.get('reg_event'), - 'recordId': '1', - 'parentFormId': 'abc-123', - 'formData': false - }; - $.ajax({ - type: 'POST', - url: ajaxUrl, - data: formData, - encode: true, - dataType: 'text' - }) - .done( function( formHtml ) { - $('.attendee-cf').html( formHtml ); - }); - }, }); // js/views/front/regTime.js @@ -933,7 +917,7 @@ app.Views.Front.RegTime = Backbone.View.extend({ 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 ); + // this.listenTo( this.model, 'change', this.render ); return this; }, @@ -1037,11 +1021,16 @@ app.Views.Front.RegTime = Backbone.View.extend({ zip: zip, country: country, }); - // console.log( newRegistrant ); + // Now we have a new registrant let's save the custom field data. + var formData = jQuery( '#attendees-cf-form-new' ).serialize(); + console.log( 'output form data' ); + console.log( formData ); + // app.saveCustomFieldsForm( + // jQuery('.attendee-cf-form').serialize() + // ); } - // return false; - // this.newRegAccount.destroy(); + this.newRegAccount.destroy(); this.newRegAccountView.remove(); app.regEventLock = false; }, @@ -1049,7 +1038,7 @@ app.Views.Front.RegTime = Backbone.View.extend({ // cancel add new registrant cancelAddNew: function(){ this.$('.glm-add-new-account').show(); - // this.newRegAccount.destroy(); + this.newRegAccount.destroy(); this.newRegAccountView.remove(); app.regEventLock = false; }, @@ -1093,32 +1082,10 @@ app.Views.Front.RegistrantForm = Backbone.View.extend({ render: function(){ this.$el.html( this.template( this.model.toJSON() ) ); // console.log( this.model.get('reg_event') ); - this.setCustomFieldsForm(); + app.setCustomFieldsForm(this.model.get('reg_event'), '.attendee-cf-new'); return this; }, - setCustomFieldsForm: function(){ - var formData = { - 'action': 'glm_members_admin_ajax', - 'glm_action': 'customFieldsFront', - 'option': 'displayForm', - 'fid': 'glm_reg_customfields_reg_event_attendee_' + this.model.get('reg_event'), - 'recordId': '1', - 'parentFormId': 'abc-123', - 'formData': false - }; - $.ajax({ - type: 'POST', - url: ajaxUrl, - data: formData, - encode: true, - dataType: 'text' - }) - .done( function( formHtml ) { - $('.attendee-cf').html( formHtml ); - }); - }, - // verify the email address verifyEmail: function(){ var email = this.$('.add_reg_email').val().trim(); diff --git a/js/models/front/regEvent.js b/js/models/front/regEvent.js index 4ca8e08..aa38b6b 100644 --- a/js/models/front/regEvent.js +++ b/js/models/front/regEvent.js @@ -19,12 +19,13 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ initialize: function(){ // for setting up classes - this.classes = new app.Collections.Front.RegClasses(); + this.classes = new app.Collections.Front.RegClasses( [] ); }, // setClasses // To setup the classes collection setClasses: function( regClasses ){ + console.log( regClasses ); this.classes.reset( regClasses ); }, @@ -44,7 +45,8 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ // Using the registrants reg_time lookup the regTime // var foundRegTime = foundClass.regTimes.findWhere({id: registrants[i].reg_time}); var foundRegTime = _.findWhere(foundClass.get('times'), { id: registrants[i].reg_time }); - // console.log( foundRegTime ); + console.log( 'find reg time' ); + console.log( foundRegTime ); if ( foundRegTime ) { // Set the reg_event for this reg_time to the class we found for registrant foundRegTime.reg_event = foundClass.get('reg_event'); @@ -60,7 +62,9 @@ app.Models.Front.RegEvent = Backbone.Model.extend({ } // Add the registrant to the regTime. var nRegTime = foundClass.regTimes.findWhere({id: hasRegTime.get('id')}); - nRegTime.registrants.create( registrants[i] ); + console.log( 'nRegTime' ); + console.log(nRegTime); + nRegTime.registrants.add( registrants[i] ); } } diff --git a/js/views/front/regClass.js b/js/views/front/regClass.js index b2d6ec6..90cba54 100644 --- a/js/views/front/regClass.js +++ b/js/views/front/regClass.js @@ -15,11 +15,14 @@ app.Views.Front.RegClass = Backbone.View.extend({ // Add times into regTimes collection var classTimes = this.model.get('times'); + var timeArs = []; for ( var key in classTimes ) { var newTime = new app.Models.Front.RegTime( classTimes[key] ); newTime.set({reg_event: this.model.get('reg_event'), parent: this.model}); - this.model.regTimes.create( newTime ); + timeArs.push( newTime ); + // this.model.regTimes.create( newTime ); } + this.model.regTimes.reset( timeArs ); // console.log( classTimes ); @@ -68,22 +71,17 @@ app.Views.Front.RegClass = Backbone.View.extend({ // this.model.set({ reg_count: this.model.registrants.length }); this.$el.html( this.template( this.model.toJSON() ) ); // Go through the RegTimes Collection and render those - // var view = this.model. - // this.model.regTimes.sort(); // var classId = this.model.id; - var view = this.model.regTimes.map(function(item){ - // item.parent = classId; - return (new app.Views.Front.RegTime({ model: item })).render().el; - }); var timeContainer = this.$('.reg-class-times'); - timeContainer.append( view ); if ( loginAccount === '' ) { this.$('.glm-add-account').hide(); } if ( !app.timeSpecific ) { // this.$('.glm-add-new-account').show(); - this.updateTimeSelection(); + // this.updateTimeSelection(); + console.log( this.model.regTimes ); + } else { var sTime = this.model.get( 'selectedTime' ); if ( !sTime ) { @@ -92,7 +90,10 @@ app.Views.Front.RegClass = Backbone.View.extend({ this.$('.glm-add-new-account').show(); } } - this.onRenderComplete(); + var view = this.model.regTimes.map(function(item){ + return (new app.Views.Front.RegTime({ model: item })).render().el; + }); + timeContainer.append( view ); return this; }, diff --git a/js/views/front/regRequestRegistrant.js b/js/views/front/regRequestRegistrant.js index 345a9ac..60617c7 100644 --- a/js/views/front/regRequestRegistrant.js +++ b/js/views/front/regRequestRegistrant.js @@ -93,7 +93,7 @@ app.Views.Front.RegRequestRegistrant = Backbone.View.extend({ this.$el.html( this.template( this.model.toJSON() ) ); this.$emailInput = this.$('.editEmail'); this.$('.reg-edit-form').hide(); - this.setCustomFieldsForm(); + app.setCustomFieldsForm(this.model.get('reg_event'), '.attendee-cf-' + this.model.id); return this; }, @@ -109,25 +109,4 @@ app.Views.Front.RegRequestRegistrant = Backbone.View.extend({ this.remove(); }, - setCustomFieldsForm: function(){ - var formData = { - 'action': 'glm_members_admin_ajax', - 'glm_action': 'customFieldsFront', - 'option': 'displayForm', - 'fid': 'glm_reg_customfields_reg_event_attendee_' + this.model.get('reg_event'), - 'recordId': '1', - 'parentFormId': 'abc-123', - 'formData': false - }; - $.ajax({ - type: 'POST', - url: ajaxUrl, - data: formData, - encode: true, - dataType: 'text' - }) - .done( function( formHtml ) { - $('.attendee-cf').html( formHtml ); - }); - }, }); diff --git a/js/views/front/regTime.js b/js/views/front/regTime.js index ad3da57..e0a0c71 100644 --- a/js/views/front/regTime.js +++ b/js/views/front/regTime.js @@ -12,7 +12,7 @@ app.Views.Front.RegTime = Backbone.View.extend({ 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 ); + // this.listenTo( this.model, 'change', this.render ); return this; }, @@ -116,11 +116,16 @@ app.Views.Front.RegTime = Backbone.View.extend({ zip: zip, country: country, }); - // console.log( newRegistrant ); + // Now we have a new registrant let's save the custom field data. + var formData = jQuery( '#attendees-cf-form-new' ).serialize(); + console.log( 'output form data' ); + console.log( formData ); + // app.saveCustomFieldsForm( + // jQuery('.attendee-cf-form').serialize() + // ); } - // return false; - // this.newRegAccount.destroy(); + this.newRegAccount.destroy(); this.newRegAccountView.remove(); app.regEventLock = false; }, @@ -128,7 +133,7 @@ app.Views.Front.RegTime = Backbone.View.extend({ // cancel add new registrant cancelAddNew: function(){ this.$('.glm-add-new-account').show(); - // this.newRegAccount.destroy(); + this.newRegAccount.destroy(); this.newRegAccountView.remove(); app.regEventLock = false; }, diff --git a/js/views/front/registrantForm.js b/js/views/front/registrantForm.js index 3123822..a43e982 100644 --- a/js/views/front/registrantForm.js +++ b/js/views/front/registrantForm.js @@ -25,32 +25,10 @@ app.Views.Front.RegistrantForm = Backbone.View.extend({ render: function(){ this.$el.html( this.template( this.model.toJSON() ) ); // console.log( this.model.get('reg_event') ); - this.setCustomFieldsForm(); + app.setCustomFieldsForm(this.model.get('reg_event'), '.attendee-cf-new'); return this; }, - setCustomFieldsForm: function(){ - var formData = { - 'action': 'glm_members_admin_ajax', - 'glm_action': 'customFieldsFront', - 'option': 'displayForm', - 'fid': 'glm_reg_customfields_reg_event_attendee_' + this.model.get('reg_event'), - 'recordId': '1', - 'parentFormId': 'abc-123', - 'formData': false - }; - $.ajax({ - type: 'POST', - url: ajaxUrl, - data: formData, - encode: true, - dataType: 'text' - }) - .done( function( formHtml ) { - $('.attendee-cf').html( formHtml ); - }); - }, - // verify the email address verifyEmail: function(){ var email = this.$('.add_reg_email').val().trim(); diff --git a/views/front/registrations/registration.html b/views/front/registrations/registration.html index 9209cc1..648545b 100644 --- a/views/front/registrations/registration.html +++ b/views/front/registrations/registration.html @@ -223,7 +223,7 @@ jQuery(document).ready(function($) {
Email Address
-
+
Address (show/hide)
@@ -372,9 +372,6 @@ 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); @@ -388,6 +385,32 @@ var app = { return false; } }, + setCustomFieldsForm: function(reg_event_id, target){ + var formData = { + 'action': 'glm_members_admin_ajax', + 'glm_action': 'customFieldsFront', + 'option': 'displayForm', + 'fid': 'glm_reg_customfields_reg_event_attendee_' + reg_event_id, + 'recordId': '1', + 'parentFormId': 'abc-123', + 'formData': false + }; + $.ajax({ + type: 'POST', + url: ajaxUrl, + data: formData, + encode: true, + dataType: 'text' + }) + .done( function( formHtml ) { + $(target).html( formHtml ); + }); + }, + saveCustomFieldsForm: function( formData, reg_event_id ){ + console.log( 'SAVING FORW DATA' ); + console.log( formData ); + console.log( reg_event_id ); + }, {if $regEvent.time_specific.value} timeSpecific: true, {else} -- 2.17.1