How to Clone an Entry?

Hi,

I would like to know how you’ll do to duplicate an entry ?
Like the jquery .clone() but with an insert in a Collection.

Creatively,
maxime

What is the goal here?

Isn’t it just a matter of

  • find
  • change ID
  • insert
2 Likes

As you can see, I would like to clone on event click on the button clone at the beginning of the line.

Single clone item

so do it?

Template

Template.<?>.events({
    'click .duplicate-item'(event, template) {
        Meteor.call('duplicate-item', this)
    }
})

Method

Meteor.methods({
    'duplicate-item'(item) {
        item._id = Random.id()
        Items.insert(item)
    }
})

I know this is horribly naive, but I don’t want to assume more than the absolute basics.

2 Likes

Thanks a lot.

Last question,
for duplicate all the items ?

that’s pretty much the same again - I think you should take a look [here] (https://www.meteor.com/tutorials/blaze/creating-an-app)

It’s only for one by one. I would like to duplicate all on single click

but isn’t it fun to learn these kinds of things yourself? :smile: I guess a simple solution would be something like this:

Meteor.methods({
    'duplicate-all-items'() {
        Items.find().fetch().forEach(item => {
            item._id = Random.id()
            Items.insert(item)
        })
    }
})

There’s probably a million better ways to do this :smile:

2 Likes

Oh ! I learned a lot on meteor,

but I don’t know nothing.

You can see an app I did : PickBack

or one another WIP : Sukul

1 Like

Whatever you learn, blog about it. You will internalize the knowledge, and better yet help out tons of people with the same issues.

Be a bro and blog about your problems and solutions.

2 Likes