Problem #1
A message display
A backend api returns an array of messages in json to the frontend as follows. Each message has an id, message text and an indicator if its unread or not.
The system:
-
should display all messages (from the json array) in a list or series of divs
-
should display all the message-text if the message is unread (unread === true) or is the 1st message. This is the "expanded" format.
-
should display only the 1st line of message-text, with ellipses (i.e. ...) at end if the message is read (unread === false) and is not the 1st message. This is the "collapsed" format. note: only display the ellipses if there is more content than 1 line i.e. would not display ellipses for message id 3 but would for message id 2
-
should expand a collapsed message when the user clicks a collapsed message
-
should collapse an expanded message when user clicks on an expanded message
Explanation
- Should display all messages (from the json array) in a list or series of divs
- I had first set routing to point
/messages
to theMessageListCtrl
controller (scripts/controllers/message-list.js
) andviews/message-list.html
template onscripts/app.js
. On the controller I used$resource
to load the data from the JSON file as if it was a RESTful service and outputted the data on the view using thengRepeat
directive over the List Group Bootstrap component.
- I had first set routing to point
- Should display all the message-text if the message is unread (unread === true) or is the 1st message. This is the "expanded" format.
- This is also explained above.
- Should display only the 1st line of message-text, with ellipses (i.e. ...) at end if the message is read (unread === false) and is not the 1st message. This is the "collapsed" format.
note: only display the ellipses if there is more content than 1 line i.e. would not display ellipses for message id 3 but would for message id 2
- I created a
collapse
filter (scripts/filters/collapse.js
) for this which trims the text and accepts a parameter to bypass it. This parameter is used passing themessage.collapsed
property inverted to it, which is initialized next to thengRepeat
directive with a default value offalse
if message is unread or is the first in the list. Also, thecollapse
filter checks if content has more than one line, and if it doesn't, returns the original value.
- I created a
- Should expand a collapsed message when the user clicks a collapsed message
- Clicking the list item inverts the boolean value of the
message.collapsed
property and, as thecollapse
filter takes that property inverted as thebypass
parameter, updates accordingly.
- Clicking the list item inverts the boolean value of the
- Should collapse an expanded message when user clicks on an expanded message
- This is also explained above.
Running it
To run the project enter the following command in your console: npm install && bower install && grunt server
. After installing all the development dependencies, it will create a local web server and open the project in the browser (thanks to Yeoman).