Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hasMany relationships fails #3833

Closed
IsaiahJTurner opened this issue Oct 8, 2015 · 6 comments
Closed

Replace hasMany relationships fails #3833

IsaiahJTurner opened this issue Oct 8, 2015 · 6 comments

Comments

@IsaiahJTurner
Copy link

Doing something like:
ama.set("questions", [Question, Question, Question]);
does not work.

@pangratz
Copy link
Member

pangratz commented Oct 8, 2015

Can you give a little bit more information? What version are you using? What does the model definition for ama look like? Best would be a reproduced example in a JSBin. Here is a starting point: http://emberjs.jsbin.com/lavacavige/edit?html,js,output

@bmac
Copy link
Member

bmac commented Oct 8, 2015

Relationships are more complex object then an array so its not possible to set the relationship property directly. Instead can use the setObjects method on the relationship.
ama.get("questions").setObjects([Question, Question, Question]);

@bmac
Copy link
Member

bmac commented Oct 8, 2015

My bad @IsaiahJTurner.

@pangratz reminded me that there is special code to handle the case of setting an array:

set: function(key, records) {
var relationship = this._internalModel._relationships.get(key);
relationship.clear();
Ember.assert("You must pass an array of records to set a hasMany relationship", isArrayLike(records));
relationship.addRecords(Ember.A(records).mapBy('_internalModel'));
return relationship.getRecords();
}

What is the error that you are seeing?

@IsaiahJTurner
Copy link
Author

@pangratz
Copy link
Member

pangratz commented Oct 8, 2015

OK, so the problem is that store.find returns a PromiseObject. This can't be added directly to a hasMany relationship. The find resolves with the specific record, which in turn can be added to the relationship.

One way to do this is via RSVP.all, which takes an array as argument and resolves when all items in the array are resolved. So the code within your action would become something like:

update: function() {
  var newBooks = [
    this.store.find("book", 4)
  ];

  var model = this.get('model');
  Ember.RSVP.all(newBooks).then(function(newBooks) {
    model.get('books').setObjects(newBooks);

    // or you can use model.set('books', newBooks);
  });
}

The thrown error message Uncaught TypeError: Cannot read property 'type' of undefined when setting a PromiseObject is not very helpful. We could add an assertion below this line and check if the contents of the array are actual instances of DS.Model.

@IsaiahJTurner do you want to submit a PR?

@pangratz
Copy link
Member

pangratz commented Nov 4, 2015

I guess this issue can be closed since an assertion for only passing instances of DS.Model has been added in #3865.

@bmac bmac closed this as completed Nov 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants