From ab7b1a6b267f390dd09b2c840aaffa271e34c678 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 21 Aug 2017 16:37:31 -0400 Subject: [PATCH] Adding initial part of backbone.js into model and view. Setting up initial backbone models and views. I'm going to go through this very soon and organize the js files. --- js/myBackbone.js | 75 +++++++++++++++++++++ models/front/registrations/list.php | 70 +++++++++++++++++-- models/front/registrations/registration.php | 24 ++++++- views/front/registrations/list.html | 6 +- views/front/registrations/registration.html | 9 ++- 5 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 js/myBackbone.js diff --git a/js/myBackbone.js b/js/myBackbone.js new file mode 100644 index 0000000..0765dbf --- /dev/null +++ b/js/myBackbone.js @@ -0,0 +1,75 @@ +jQuery(document).ready(function($){ + // Backbone.js + // Model (Test) + + var EventReg = Backbone.Model.extend({ + // Default todo attrubute values + defaults: { + event_name: '', + descr: '' + } + }); + + var EventRegList = Backbone.Collection.extend({ + model: EventReg + }); + + var EventRegView = Backbone.View.extend({ + + tagName: 'div', + + className: 'glm-reg-event-item clearfix', + + eventRegTpl: _.template( + '

<%= event_name %>

' + + '
' + + '' + + '
' + + '

<%= descr %>

' + ), + + // Called when the view is first created + initialize: function(){ + this.listenTo(this.model, 'change', this.render); + }, + + render: function(){ + this.$el.html( this.eventRegTpl( this.model.toJSON() ) ); + return this; + }, + + }); + + var EventListView = Backbone.View.extend({ + + el: '#eventapp', + + initialize: function(){ + this.collection.bind('add', this.onModelAdded, this); + this.render(); + }, + + + onModelAdded: function(item) { + var eventView = new eventView({model: item}); + this.$el.append( eventView.render().el); + }, + + render: function(){ + var eventView = this.collection.map(function(item){ + return (new EventRegView({model: item})).render().el; + }); + this.$el.append(eventView); + return this; + }, + + }); + + // Use the registration object to build new EventReg + var event1 = new EventReg({ 'event_name': registration.event_name, 'descr': registration.descr }); + + var eventList = new EventRegList( [ event1 ] ); + + var eventRegView = new EventListView({ collection: eventList }); + +}); diff --git a/models/front/registrations/list.php b/models/front/registrations/list.php index 303649f..2b2cdc7 100644 --- a/models/front/registrations/list.php +++ b/models/front/registrations/list.php @@ -26,6 +26,13 @@ // Save plugin configuration object $this->config = $config; + /* + * Run constructor for the REgistrations data class + * + * Note, the third parameter is a flag that indicates to the Contacts + * data class that it should flag a group of fields as 'view_only'. + */ + parent::__construct(false, false, true); } /** @@ -56,10 +63,16 @@ public function modelAction($actionData = false) { - $start = 1; - $limit = 20; - $where = 'true'; - $alphaWhere = ''; + $start = 1; + $limit = 20; + $where = ''; + $alphaWhere = ''; + $alphaList = ''; + $paging = ''; + $nextStart = ''; + $alphaSelected = false; + $haveRegEvents = false; + $regEventsCount = false; // Get any provided option if (isset($_REQUEST['option'])) { @@ -83,10 +96,41 @@ default: // Get a current list of reg events - $listResult = $this->getSimpleRegEventsList($where.$alphaWhere, 'event_name', true, 'id', $start, $limit); + $listResult = $this->getSimpleRegEventsList($where.$alphaWhere, 'event_name', true, 'id', $start, $limit, true); echo '
$listResult: ' . print_r( $listResult, true ) . '
'; - break; + // Get paging results + $numbDisplayed = $listResult['returned']; + $lastDisplayed = $listResult['last']; + if ($start == 1) { + $prevStart = false; + } else { + $prevStart = $start - $limit; + if ($start < 1) { + $start = 1; + } + } + if ($listResult['returned'] == $limit) { + $nextStart = $start + $limit; + } + + // since we're doing paging, we have to break out just the event data + $list = $listResult['list']; + unset($listResult); + + // If we have list entries - even if it's an empty list + $success = true; + $haveRegEvents = false; + if ($list !== false) { + + $success = true; + + // If we have any entries + if (count($list) > 0) { + $haveRegEvents = true; + } + } + break; } // including test data for now @@ -94,7 +138,19 @@ // Compile template data $templateData = array( - 'events' => $eventData, + //'events' => $eventData, + 'regEventsCount' => $regEventsCount, + 'haveRegEvents' => $haveRegEvents, + 'regEvents' => $list, + 'alphaList' => $alphaList, + 'alphaSelected' => $alphaSelected, + 'numbDisplayed' => $numbDisplayed, + 'lastDisplayed' => $lastDisplayed, + 'paging' => $paging, + 'prevStart' => $prevStart, + 'nextStart' => $nextStart, + 'start' => $start, + 'limit' => $limit, ); // Return status, any suggested view, and any data to controller return array( diff --git a/models/front/registrations/registration.php b/models/front/registrations/registration.php index 99b3262..08f0951 100644 --- a/models/front/registrations/registration.php +++ b/models/front/registrations/registration.php @@ -26,10 +26,25 @@ // Save plugin configuration object $this->config = $config; + /* + * Run constructor for the REgistrations data class + * + * Note, the third parameter is a flag that indicates to the Contacts + * data class that it should flag a group of fields as 'view_only'. + */ + parent::__construct(false, false, true); } public function modelAction($actionData = false) { + wp_register_script( + 'mybackboneapp', + GLM_MEMBERS_REGISTRATIONS_PLUGIN_URL . 'js/myBackbone.js', + array( 'jquery', 'backbone', 'underscore' ), + '1.0', + true + ); + wp_enqueue_script( array( 'backbone', 'jquery', 'mybackboneapp' ) ); $regEvent = array(); // Get any provided option @@ -61,6 +76,12 @@ $view = 'registration'; switch ( $option ) { + default: + // Get the RegEvent entry + $entry = $this->getEntry( $eventRegID ); + //echo '
$entry: ' . print_r( $entry, true ) . '
'; + + break; } @@ -76,7 +97,8 @@ // Compile template data $templateData = array( - 'events' => $regEvent, + 'eventReg' => $entry, + 'events' => array() ); // Return status, any suggested view, and any data to controller return array( diff --git a/views/front/registrations/list.html b/views/front/registrations/list.html index ecc7bf6..adde015 100644 --- a/views/front/registrations/list.html +++ b/views/front/registrations/list.html @@ -1,8 +1,8 @@
- {foreach $events as $event} + {foreach $regEvents as $event}
-

{$event.title}

-

{$event.short_desc}

+

{$event.event_name}

+

Short description will go here.

{/foreach} diff --git a/views/front/registrations/registration.html b/views/front/registrations/registration.html index 1f1f640..7d61869 100644 --- a/views/front/registrations/registration.html +++ b/views/front/registrations/registration.html @@ -1,4 +1,5 @@ -
+
+