Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Within a custom()-method: How to save to a different field within an embedded array? #1219

Closed
thebarty opened this issue Sep 24, 2015 · 3 comments

Comments

@thebarty
Copy link

Hi guys,

I am building a icecream-order-machine where customers place their icecream-orders. 👍

There are 2 Collections:

  1. Scoops (= our inventory, types of scoops)
  2. ScoopOrders (= a customer can order multiple amounts of scoops).
    An example: "Jimmy Hendrix orders 2 scoops of vanilla and 3 scoops of chocolate."

The orderItems are embedded as an array into the ScoopOrder Collection.

Question:
We are doing MongoDB-NoSQL, so the best way to go is to save the scoop._id PLUS the full ScoopObjekt for each ScoopOrder.

How do I do this with Collection2 & Autoform?

This is the code I have so far - I am stuck within the custom() method:

AutoformScoopOrders = new Mongo.Collection('autoformscooporders');

autoformScoopOrdersSchema = new SimpleSchema({
  customerName: {
    type: String
  },

  // scoopItems Array
  scoopItems: {
    type: Array,
    optional: true,
  },
  'scoopItems.$': {
    type: Object
  },
  'scoopItems.$.amount': {
    type: Number,
  },
  'scoopItems.$.scoopId': {
    type: String,
    autoform: {  // Foreign-Key Relationship to single Scoops-Object
      options: function () {
        return Scoops.find().map(function (c) {
          return {label: c.name + '( ' + c.price + '$ )', value: c._id};
        });
      }
    },
    custom: function () {
      if (this.isInsert || this.isUpdate) {
        var theScoopInstance = Scoops.findOne(this.value);
        **// QUESTION: How do I save the instance to scoopItems.$.scoopInstance?**
      }
    },
  },
  'scoopItems.$.scoopInstance': {
    type: Object,
    optional: true,
    autoform: {
      type: 'hidden',
      label: false,
    },
  },  
});

AutoformScoopOrders.attachSchema(autoformScoopOrdersSchema);
@serkandurusoy
Copy link

Try this, or something similar since I have not tested this, but it should point you in the right direction.

AutoformScoopOrders = new Mongo.Collection('autoformscooporders');

autoformScoopOrdersSchema = new SimpleSchema({
  customerName: {
    type: String
  },
  // scoopItems Array
  scoopItems: {
    type: Array,
    optional: true,
  },
  'scoopItems.$': {
    type: Object
  },
  'scoopItems.$.amount': {
    type: Number,
  },
  'scoopItems.$.scoopId': {
    type: String,
    autoform: {  // Foreign-Key Relationship to single Scoops-Object
      options: function () {
        return Scoops.find().map(function (c) {
          return {label: c.name + '( ' + c.price + '$ )', value: c._id};
        });
      }
    }
  },
  'scoopItems.$.scoopInstance': {
    type: Object,
    blackbox: true // or define schema keys for instance properties if you want
    optional: true,
    autoValue: function () {
        var theScoopInstanceId = this.siblingField('scoopId');
        var theScoopInstance = theScoopInstanceId.isSet && Scoops.findOne(theScoopInstanceId.value);
        return _.omit(theScoopInstance, '_id');
    },
    autoform: {
      omit: true;
    },
  },  
});

@thebarty
Copy link
Author

Hi @serkandurusoy

yeah man! Got it going:

  'scoopItems.$.scoopInstance': {
    type: Object,
      blackbox: true, // or define schema keys for instance properties if you want
      optional: true,
      autoValue: function () {
          var theScoopInstanceId = this.siblingField('scoopId');
          var theScoopInstance = theScoopInstanceId.isSet && Scoops.findOne(theScoopInstanceId.value);
          return _.omit(theScoopInstance, '_id');
      },
      autoform: {
        omit: true,
      },
  },

Now I'll try to use collection-hooks to update the scoopInstance on update/deletes on the Scoops Object.

Thanks a lot for your help!

@serkandurusoy
Copy link

No problem, glad it worked!

Sent from my iPhone

On Sep 28, 2015, at 10:36, MikeSmith12222 notifications@github.com wrote:

Hi @serkandurusoy https://github.com/serkandurusoy

yeah man! Got it going:

'scoopItems.$.scoopInstance': {
type: Object,
blackbox: true, // or define schema keys for instance properties
if you want
optional: true,
autoValue: function () {
var theScoopInstanceId = this.siblingField('scoopId');
var theScoopInstance = theScoopInstanceId.isSet &&
Scoops.findOne(theScoopInstanceId.value);
return _.omit(theScoopInstance, '_id');
},
autoform: {
omit: true,
},
},

Now I'll try to use collection-hooks to update the scoopInstance on
update/deletes on the Scoops Object.

Thanks a lot for your help!


Reply to this email directly or view it on GitHub
#1219 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants