r/programminghelp • • Dec 29 '19

MVC

Hello, Im currently working on a school task and having a bit of trouble. Im supposed to create an MVC, but as far as I understand it, MVC is a sort of way to write your system, a way for the components to interact with oneanother. Am I correct in this?

If I am correct in this, do I need a context for the MVC to be written within? Because I've been rereading the assessment over and over but they dont seem to give any sort of context, so I dont understand how Im supposed to create the MVC. Can i create an MVC without a context? Is the MVC a context in of itself?

Edit: this is the assignmrnt description for context

3 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/_Foxtrot_ Dec 29 '19

I've tried to create a simple example of MVC, please do not use this for your assignment (you're welcome to take it and add to it or change it). https://pastebin.com/T1T1Nead

The context they're presenting you with is something along the lines of "hey you have access to all of this data, now go make something meaningful out of it." Maybe rephrase "context" as "what did the customer ask for?" or "what are we building?" Take https://observablehq.com/@d3/sortable-bar-chart for example.

Everything you see on that page is the view. When you click the dropdown list to change the sort order, the view is sending an update event to the controller. Something along the lines of...

- "Hey controller, I just received a request from the user to sort this in alphabetical order, but I don't know how to do that, so can you just reformat the data for me and then I'll just show them whatever you gave me."

- The controller gets that request and says "Okay, let me go get data from the model (or maybe it already has it stored in a variable)."

- The model receives a request for data, and it returns that data. It's pretty stupid, just like the view.

- The controller receives data from the model, and remembers that the view asked it to sort it alphabetically, does so and returns it to the view.

- The view, still oblivious to everything that has happened, displays whatever it was returned by the controller.

In the example I wrote, I'm doing *presentation logic* in the view. I want to know what was returned so I can custom-tailor the response. Hope this helps.

2

u/Clint_Bolduin Dec 29 '19

Thank you so much for this extensive response, explenation and example! From what I can see looking at it now, this definitely will be helpful! I'll give my assignment another shot tomorrow morning (it's late here so I'll be going to bed right about now)

I will of course not hand in your code for the assessment, I strive to understand this and just handing in your code wont help me do that so don't worry about that.

2

u/_Foxtrot_ Dec 29 '19

Awesome. If you have any more questions feel free to shoot me a DM or continue this comment thread.