UMass Lowell Dept. of Computer Science

COMP 4620 — GUI Programming II

Spring 2016 Semester, Section 201

Prof. Jesse M. Heines

Notes for Class No. 8

Moving Data Out of Views and Into Controllers

Thursday, February 11, 2016

A video of this class is (or will be) posted at:  http://echo360.uml.edu/heines2016/comp4620-201.html


Handouts and Materials


Openings / Announcements / Reminders

Proposal resubmissions are due next Thursday, but may be resubmitted at any time

Justin Nguyen — please see me re our recent email exchange


Class Notes

Related reading for this class:  GetMEAN: Chap. 4


Concepts (p. 106)

We ended our last class with all the content and data still hard-coded in the views.

In “the MVC architecture, the model holds the data, the controller then processes the data, and finally the view renders the processed data.”

Approach


Updating the Home Page (p. 107)

Current code in file loc8r/app_server/views/locations-list.jade (the view)

Current code in file loc8r/app_server/controllers/locations.js (the controller)

The first step is to update the controller to pass the data that is currently in the view

We can now update the view to use the data passed from the controller


Coding Repeating Data (p. 109)

Lists often display data in the same pattern in the same layout, and those lists are most often coded as arrays

The loc8r home page display data on each location in the following format

These data can be encoded in a JavaScript object as follows

An array of such objects is then added to the controller


Jade code to step through each element in the locations array (p. 111)

Original code in file loc8r/app_server/views/locations-list.jade

Wrapping in a loop


Jade code to output the rating stars (p. 113)

Desired result

Original code (hard-coded)

Using location.rating, which is passed into the template by the homelist function, as in:

locations: [{
    name: 'Starcups',
    address: '125 High Street, Reading, RG6 1PS',
    rating: 3,
    facilities: ['Hot drinks', 'Food', 'Premium wifi'],
    distance: '100m' ,
    detailpage: 'starcups-detail'   // added by JMH on January 3, 2016 at 8:58 PM
}, { ...


Jade Includes and Mixins (pp. 113-115)

Both of these are reusable components


A mixin is essentially a function

The following code wraps the rating stars routine above in a mixin

This mixin is then called simply by placing a + sign before its name

+outputRating( location.rating )

Mixins should be inserted in Jade files immediately following the extends statement, and before the block content statement


An include is essentially a C-like #include

Holmes creates a subfolder of the app_server/views folder named _includes

To use the mixin in the external file, Holmes then replaces the the mixin in the above code with the statement:

include _includes/sharedHTMLfunctions

Note typo on page 115:  file locations-listings.jade should be locations-list.jade


Finishing Up (p. 117)

“Appendix C goes through the process for each of the three remaining pages, showing what the controller and view code should look like for each one.”

“The end goal is to have no data in any of the views.”

Process for each page

  1. Look at the data in the view.
  2. Create a structure for that data in the controller.
  3. Replace the data in the view with references to the controller data.
  4. Look for opportunities to reuse code.

My implementation

http://peaceful-shelf-9605.herokuapp.com/


Please read Chapter 5 before our next class, as we will begin looking at MongoDB and Mongoose next week



This is document http://jesseheines.com:8080/~heines/91.462/91.462-2015-16s/462-lecs/lecture08.jsp.  It was last modified on Friday, August 26, 2022 at 4:09 PM.
Copyright © 2022 by Jesse M. Heines.  All rights reserved.  May be freely copied or excerpted for educational purposes with credit to the author.