0

I am curious what the best approach might be to create a button in meteor to duplicate an array object. As an example if I had:

"TasksWithData": [
  {
    "inspectionDetails": [
      {
        "name": "somename",
        "inspector": "Ss6TjGGzqWJRZYStx",
        "inspectorName": "name of inspector",
        "projectType": "inspection",
        "startDate": "2017-01-12T05:00:00.000Z",
        "desc": "a description",
        "activeTask": true
      }
    ],
    "TaskItem": [
      {
        "DataFacilitySection": "dsfgsdfgds",
        "DataItemDescription": "item 2",
        "DataItemSection": "dfgdfgdf",
        "DataItemCode": "dfgdfgdf",
        "DataItemPass": null
      }
    ],
  }
],

If I wanted to duplicate the entire TasksWithData Array (which is a sub-document and does not have an _id) with a client button...how would I do this?

Here is the event I am calling:

  'click .duplicate': function(){
    Meteor.call('duplicateItem', this._id);
  }

and here is the template structure:

Inspections/AddInspectionsHome:

<template name="AddInspectionHome">
  <div class="container single-list conatiner-padding">
    <h1>Task Navigator</h1>
    <hr>
      <div class="panel panel-default">
        <div class="panel-heading no-padding">
          <span>Select a Client</span>
        </div>
        {{#each findClients}}
          {{> SingleClientInspection}}
        {{/each}}
      </div>
  </div>
  <!--TODO: Need to add the ability to duplicate these inspections-->
</template>

Inspections/SingleClientAddInspection (the ReactiveVar Mode that triggers the users ability to see active inspections):

{{#if activeMode}}
    <div class="panel panel-default col-lg-12">
      <a href="#" class="active btn btn-primary">Close</a>
      <div class="panel panel-default">
        <div class="panel-heading">View Active Tasks</div>
          <table class="table table-striped">
            <thead>
              <tr>
                <th>Facility</th>
                <th>Inspector</th>
                <th>Type</th>
                <th>Due Date</th>
              </tr>
            </thead>
            <tbody>
              {{#each TasksWithData}}
                {{#each inspectionDetails}}
                  {{#if activeTask}}
                    <tr>
                      <td>{{TaskFacility}}</td>
                      <td>{{inspectorName}}</td>
                      <td>{{projectType}}</td>
                      <td>{{startDate}}</td>
                      <td><a href="#" class="duplicate btn btn-success">Copy</a></td>
                    </tr>
                  {{/if}}
                {{/each}}
              {{/each}}
            </tbody>
          </table>
      </div>
    </div>
  {{/if}}

3
  • 1
    Duplicate it to where? To another mongo document? To a temporary variable? To another key within the same mongo document? Commented Jan 12, 2017 at 21:37
  • Actually, I really would like to just add it as a new identical object to the same array. The client wants the ability to duplicate a previously set up inspection so they don't have to re-add them from scratch. All of these inspections really need to live in the same array like above. Commented Jan 12, 2017 at 22:47
  • Also I edited my above code. My bad. I forgot to close of the parent object. Commented Jan 12, 2017 at 23:04

1 Answer 1

2

I suggest first extracting the original array then concatenating it back onto itself.

const doc = MyCollection.findOne(_id); // however you get your original doc
let TasksWithData = doc.TasksWithData;
TasksWithData.concat(TasksWithData);
MyCollection.update(_id,{$set: {TasksWithData: TasksWithData}});
Sign up to request clarification or add additional context in comments.

5 Comments

Great thanks I will give this a shot as a method tied to a click event. Will report back tomorrow! Thanks for taking the time man!
Well this feels like its getting close, but I have a few issues I need to get through. I will update my original post and see what you think! Again thanks for the help on this one!
Since your event handler is running on the array itself you can get the _id of the parent object with Template.parentData(1)._id
Thank sweet 7lb 6oz BABY JESUS! This is exactly what I have been lacking for more than just this scenario! I really actually like Blaze, and hope MDG finds a way to keep it around, but DOM traversal (or in this case template) has been a major sticking point for me. Is there a good place in the docs that I can read more on these? Cheers brother and thanks for the effort!
Hey @Michel I actually have one small issue using this method. Since you were so great at responding (and I have a tight deadline haha) might you be able to take a look at this related issue? autofom update not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.