Is there an 'array pointer' in model loop or set() function behaves like computed?


#1

Hello there I’ve spend now 10h on this and still couldn’t figured out what’s going on.

I have 2 components:

{{task-component task=visitor.task}}

{{answer-component task=visitor.task}}

Relationship for models:

Visitor: task: DS.hasMany(‘task’, {inverse: ‘visitor’})

Task: visitor: DS.belongsTo(‘visitor’,{inverse:‘task’})

Everything works fine however when I’m using in task-component:

didReceiveAttrs() {
        let defaultAnswers = this.get('questions.answers'); // question is service, answer is property only used here
        this.get('task').forEach(function(item) {
            item.set('answers',defaultAnswers); // <- this is the issue
        });
    },

It’s binding defaultAnswers to answer attribute for all of task data (visitor have oneToMany relation).

After going to ‘answer-component’ -> answer-item ( which is in handelbars loop for task) -> filling form -> set(‘task.answer’,answer) -> all of task.answer are updated.

Any idea why this happens ?


#2

see: https://emberjs.com/api/ember/2.18/classes/@ember%2Fobject%2Finternals/methods/copy?anchor=copy

item.set('answers', copy(defaultAnswers));

#3

Thanks a lot @nightire ! Works like a charm ^.^