Backbone Collection create does not work with an existing model and wait set to true #3028

Closed
lfac-pt opened this Issue Feb 27, 2014 · 14 comments

9 participants

@lfac-pt

Please checkout this jsfiddle http://jsfiddle.net/GSs8H/1/

An error Uncaught Error: A "url" property or function must be specified is thrown because _prepareModel no longer sets the the model's collection which is problematic because add is only called after the server response.

This problem was introduced between 1.1 and 1.1.1.

I can try to make a PR with a fix, if this is indeed a bug.

@magalhas

This started happening for me to at 1.1.1. 1.1.2 is still affected.

@lfac-pt

Yes, I also had this problem with 1.1.2 and with master.

@tgriesser tgriesser added bug and removed bug labels Mar 2, 2014
@tgriesser
Collaborator

I'm unsure whether this actually a bug, or whether previously undocumented behavior for create is now unsupported. The documentation states that the arguments to for create are collecton.create(attributes, [options]).

And from the docs:

Convenience to create a new instance of a model within a collection. Equivalent to instantiating a model with a hash of attributes, saving the model to the server, and adding the model to the set after being successfully created.

Which would indicate it's not expecting a model instance. Do you have an example use case of needing create the way you're using it and seeing the issue?

@lfac-pt

The docs also says:

The create method can accept either an attributes hash or an existing, unsaved model object.

@tgriesser tgriesser added the bug label Mar 2, 2014
@tgriesser
Collaborator

Ah, missed that, bug then (docs or otherwise).

@jashkenas
Owner

Anyone know which commit introduced the regression?

@tgriesser
Collaborator

Yeah, seems it was here: #2838

Didn't realize that having the collection attribute set on the model was relied upon for any reason before the model was added to the collection.

@jashkenas
Owner

@tgriesser ... so do you think it'll be easy to fix? Or is it a fundamental problem with the addReference idea?

@tgriesser
Collaborator

Yeah I think it's more of a fundamental problem with the addReference idea.

It feels a bit odd that you'd need the model to have knowledge of its collection before being able to save it, when it doesn't yet exist in that collection.

Let me see if there's anything I can come up with here.

@tgriesser tgriesser added a commit to tgriesser/backbone that referenced this issue Mar 3, 2014
@tgriesser tgriesser Fixes #3028 assign collection in prepareModel
Also removes model.collection in cases of failure
with wait:true. Presumably, attempting to add a model to a
collection shouldn't set the "collection" attribute
on a model if it's not actually added.
4952527
@tgriesser tgriesser added a commit to tgriesser/backbone that referenced this issue Mar 3, 2014
@tgriesser tgriesser Fixes #3028 assign collection in prepareModel
Also removes model.collection in cases of failure
with wait:true. Presumably, attempting to add a model to a
collection shouldn't set the "collection" attribute
on a model if it's not actually added.
28fd5ac
@tgriesser tgriesser closed this in f5dba8e Mar 18, 2014
@gildorwang

Hi, we hit this issue in our product. The edge version seems working for us now. When are you going to publish the fix to the official package and increase the version number?

@hansgill

Any idea when this will be pushed to an official package with a new version? I just ran into this issue on 1.1.2 as well.

@slavajacobson

This is still a problem. How come this issue keeps on getting closed?

@jridgewell
Collaborator

@slavajacobson: Can you provide a test case?

@FaKeller

I just came across this issue as well and can confirm that it occurred in my case using Backbone 1.1.2, which is the version used in the fiddle opening the issue.

After updating Backbone to 1.2.1 the issue did not occur anymore and everything worked as expected. You can see for yourself in the modified fiddle: http://jsfiddle.net/1jtk8nuo/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment