简体   繁体   English

会话中流星存储和检索自定义对象

[英]Meteor Storing and Retrieving Custom Objects in Session

I have a custom shopping cart object that I created and put it in the lib folder. 我创建了一个自定义购物车对象,并将其放在lib文件夹中。

ShoppingCart = function ShoppingCart() {
  this.Items = new Array();
  this.grandTotal = 0.00;
}

ShoppingCart.prototype.addItem = function(Item){
  this.Items.push(Item);
  this.Items.sort();
  this.calculateTotal();
}

I initialized the shopping cart and store it as Session.set('shoppingCart') during the page created phase. 我初始化了购物车,并在页面created阶段将其存储为Session.set('shoppingCart')

Template.loginStatus.created = function() {
  Session.set('loginShown',false);
  if(!Session.get('shoppingCart')){ //set default if session shopping cart not exist
    var cart = new ShoppingCart();
    Session.setDefault('shoppingCart',cart);
  }

Then when user click add item to cart, it will trigger this logic: 然后,当用户单击将商品添加到购物车时,它将触发以下逻辑:

var cart = Session.get('shoppingCart');
cart.addItem(item);
Session.set('shoppingCart',cart);

Somehow, it does not work. 不知何故,它不起作用。 When I take a look ad the chrome console it says undefined is not a function , pointing at cart.addItem(item) line. 当我在chrome控制台上看广告时,它说undefined is not a function ,指向cart.addItem(item)行。 If I change it to this, it will work , but of course since everytime new shopping cart is created, I cannot accumulate items in the cart. 如果将其更改为此,它将起作用,但是当然,由于每次创建新的购物车时,我都无法在购物车中累积商品。

var cart = new ShoppingCart();
cart.addItem(item);
Session.set('shoppingCart',cart);

How should I store and retrieve the object from session properly? 我应该如何正确地从会话中存储和检索对象? It looks like the returned object from the Session.get() somehow not considered as ShoppingCart. 它看起来像是从Session.get()返回的对象,以某种方式不视为ShoppingCart。 Did I miss any type cast? 我想念任何类型的演员吗?

As @Peppe LG mentioned, you can only store EJSONs in Session. 正如@Peppe LG提到的那样,您只能在会话中存储EJSON。 To store your custom object, you need to be able to manually transform it to and from EJSONs. 要存储您的自定义对象,您需要能够手动将其与EJSON相互转换。 Example: 例:

_.extend(ShoppingCart, {
  fromJSON: function(json) {
    var obj = new ShoppingCart();
    obj.grandTotal = json.grandTotal;
    obj.Items = json.Items;
    return obj;
  },
});

_.extend(ShoppingCart.prototype, {
  toJSON: function() {
    return {
      grandTotal: this.grandTotal,
      Items: this.Items,
    };
  },
});

Then you can save it to Session: 然后,您可以将其保存到会话:

Session.set('shoppingCart', cart.toJSON());

and restore: 并还原:

ShoppingCart.fromJSON(Session.get('shoppingCart'));

I ran into the same problem. 我遇到了同样的问题。 Essentially what is happening Meteor Sessions (and Collections) can only store EJSON types, so your ShoppingCart custom type is retrieved from the Session as a normal Object. 本质上,正在发生的流星会话(和集合)只能存储EJSON类型,因此,您从Shopping Session中检索到的ShoppingCart自定义类型是普通对象。

While you can manually transform to and from EJSONs, you may end up needing to do this repeatedly in a lot of different places. 虽然您可以手动与EJSON进行相互转换,但最终可能需要在许多不同的地方重复进行此操作。 If your ShoppingCart is a member of another object, you'll have to also manually transform the member. 如果您的ShoppingCart是另一个对象的成员,则还必须手动转换该成员。 It's better to use EJSON.addType to tell Meteor how to handle it automatically anywhere you store or retrieve an object of that type. 最好使用EJSON.addType告诉Meteor如何在您存储或检索该类型的对象的任何地方自动处理它。

There's a great demo of this here: https://www.eventedmind.com/feed/meteor-create-a-custom-ejson-type . 这里有一个很棒的演示: https : //www.eventedmind.com/feed/meteor-create-a-custom-ejson-type Full docs are also here: http://docs.meteor.com/#/full/ejson . 完整文档也位于此处: http : //docs.meteor.com/#/full/ejson But a short version is this: 但是这是一个简短的版本:

  1. Add a method to your custom type called typeName: 在自定义类型中添加一个名为typeName的方法:

    \nShoppingCart.prototoype.typeName = function(){ ShoppingCart.prototoype.typeName = function(){\n    return "ShoppingCart"; 返回“购物车”;\n}; };\n
  2. Add another method called toJSONValue: 添加另一个方法toJSONValue:

    \nShoppingCart.prototype.toJSONValue = function(){ ShoppingCart.prototype.toJSONValue = function(){\n    /* return a JSON compatible version of your object */ / *返回对象的JSON兼容版本* /\n}; };\n
  3. And finally, add the custom type to EJSON with: 最后,使用以下命令将自定义类型添加到EJSON:

    \nEJSON.addType("ShoppingCart", function fromJSONValue(value){ EJSON.addType(“ ShoppingCart”,function fromJSONValue(value){\n    /* return an object of your custom type from the JSON object 'value' */ / *从JSON对象“值”返回您自定义类型的对象* /\n}; };\n

NOTE: the "Type Name" in steps 1 and 3 must match exactly. 注意:步骤1和3中的“类型名称”必须完全匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM