Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJeremy Bradrick
6,731 PointsWhy is stuff "obliterated"?
In the video Kenneth says he knows some stuff will be completely obliterated below and then he modifies the save(). I don't understand what and why it will be obliterated. Can someone explain that bit?
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe save
is sending cookie information back to the browser. The issue is what happens if the cookie being held by the browser already has some data in the json object stored in that cookie? If the response ignores this pre-existing cookie data, it will get overwritten ("obliterated") by the new cookie in the response.
Kenneth's solution is to extract the existing cookie info from the request, update only the parts that have changed via the form, then send the updated cookie info back in the response.
This is a Read-Modify-Write flow that is typically used when updating partial information.
matt23lv
9,404 PointsI wish all of that would be in the Video, so far this is the most unclear tutorial on TreeHouse what I have seen. I am like reading topics here on the forum to understand what is going on in Videos.
john larson
16,594 Pointsjohn larson
16,594 PointsNice concise answer Chris.
Matthew Scott
5,525 PointsMatthew Scott
5,525 PointsThanks for this!
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsI wonder though what I need to do to reproduce this obiliteration. Just so I can understand what's going on. I tested out the cookie data before applying the changes Kenneth made and didn't have any problems with the key value pairs not changing or losing that data. (I've since updated the code to the state at the end of the video.
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsHey Jonathan Grieve, great followup question! In the current code set, "obliteration" will not happen since the form contains the entire data set from name to all the accessories. This means if no saved data was used, it would still work since the form is keeping all data alive.
I modified the code to verify all the data from the saved fields is present in the form data. The form data effectively overwrites all of the saved data, so a merge with saved, is moot.
Now if some of the data on the page was saved in cookies but not covered by the form, than that data would get obliterated.
It's possible to contrive an example from the current code:
Again, under the current code, if accessories are chosen on the
builder.html
page and "update" is selected, all data is kept in the cookies. Even if you go back in history to theindex.html
page, then hit 'Let's build it" to restart, all previously selected accessories are remembered. However, if you add the line below to thesaved()
function, it will ignore the saved data:Now, visit the builder page to update accessories, followed by going back to the index page, and hitting "Let's build it". This time all of the previously selected accessories on the builder page will have been forgotten. This is because the
index.html
page only updates thename
field. Since the accessory data wasn't moved fromsaved
data to the "returning cookie", the builder only has the "name" passed through.Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsThanks Chris Freeman I think I understand! :)
I'll have a crack at that soon!