Partially update a session variable in meteor?

fuzzybabybunny

I think that I might be missing something. I've got the entire contents of a user's product order form stored in a session variable.

var orderFormContents = {
  numDoors: 4
  numWheels: 4
  numSeats: 5
};

Session.set("orderFormContentsSessionVar", orderFormContents);

How do I update the value of just one key in orderFormContentsSessionVar, for instance, just numDoors?

I don't want to overwrite the entire existing contents of the session var.

I would love to be able to do something like:

Session.set("orderFormContentsSessionVar.numDoors", 2);

Something equivalent to _.extend

UPDATE

Following the example of the answer below, I just wrote a function for it:

var updateSession = function(sessionVarName, updateParams){
  var obj = Session.get(sessionVarName);
  _.extend(obj, updateParams);
  Session.set(sessionVarName, obj);
  console.log("updated session name: ", sessionVarName, "new session contents: ", Session.get(sessionVarName));
};
Rafael Quintanilha

Using like you proposed:

var obj = Session.get("orderFormContentSessionVar");
Session.set("orderFormContentsSessionVar", _.extend(obj, {numDoors: 2}));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related