Updating things for the custom fields.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 15 Dec 2017 21:32:56 +0000 (16:32 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 15 Dec 2017 21:32:56 +0000 (16:32 -0500)
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
js/models/front/regEvent.js
js/views/front/regClass.js
js/views/front/regRequestRegistrant.js
js/views/front/regTime.js
js/views/front/registrantForm.js
views/front/registrations/registration.html

index 39ca6b7..1c63d32 100644 (file)
@@ -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();
index 4ca8e08..aa38b6b 100644 (file)
@@ -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] );
                 }
 
             }
index b2d6ec6..90cba54 100644 (file)
@@ -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;
     },
 
index 345a9ac..60617c7 100644 (file)
@@ -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 );
-        });
-    },
 });
index ad3da57..e0a0c71 100644 (file)
@@ -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;
     },
index 3123822..a43e982 100644 (file)
@@ -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();
index 9209cc1..648545b 100644 (file)
@@ -223,7 +223,7 @@ jQuery(document).ready(function($) {
                     <div class="columns large-4 small-12">Email Address</div>
                     <div class="columns large-8 small-12"><input class="add_reg_email" value="<%- email %>"></div>
                 </div>
-                <form class="attendee-cf-form"><div class="attendee-cf"></div></form>
+                <form id="attendee-cf-form-new"><div class="attendee-cf-new"></div></form>
                 <div>
                     <a class="show-hide-address">Address (show/hide)</a>
                 </div>
@@ -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}