Skip to content

How to clone a Model? #2702

Answered by kubk
bymoe asked this question in General
Jan 7, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

It's up to you how to implement the clone method according to your business rules:

class TodoStore {
  ...
  
  clone() {
    const clone = new TodoStore();
    clone.todos = this.todos; // Or use Object.assign here
    return clone;
  }
}

The logic of duplication might be different. For example, I could use autoincrement ID instead of UUID:

class TodoStore {
  ...
  
  clone() {
    const clone = new TodoStore(this.id + 1);
    clone.todos = this.todos;
    return clone;
  }
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bymoe
Comment options

Answer selected by bymoe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants