How to clone a Model?
#2702
-
Let's say I have a simple Model with observable
now I want to add a feature to clone this todo just by duplicate this exact model with new generated id with it's todos and any extra infos. How to do that? |
Beta Was this translation helpful? Give feedback.
Answered by
kubk
Jan 7, 2021
Replies: 1 comment 1 reply
-
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;
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
bymoe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's up to you how to implement the clone method according to your business rules:
The logic of duplication might be different. For example, I could use autoincrement ID instead of UUID: