Skip to content

Using Model's collection option does not behave intuitively #2714

Closed
@furf

Description

@furf

When a Model is created and added to a collection then saved, the model is added to the collection's model properties and the model's events are propagated to the collection.

var c = new Collection({ model: Model });
var m = new Model({});
c.add(m);
m.save();

However, when a model is created using the collection option and saved, it is not added to the collection nor are the events propagated to the model and related collection events triggered.

var c = new Collection({ model: Model });
var m = new Model({}, { collection: c });
m.save();

IMHO, this behavior is less than intuitive. I assumed that the collection option would ultimately bind the model to the collection once saved, but it doesn't. If the only purpose is to pass along the collection's url, why not just pass it literally? (As recommended in the docs.)

If you pass a {collection: ...} as the options, the model gains a collection property that will be used to indicate which collection the model belongs to, and is used to help compute the model's url. The model.collection property is otherwise added automatically when you first add a model to a collection.

{url: "..."} and/or {urlRoot: "..."} options may be passed when creating a new model that needs to have a custom one-off URL endpoint.

var m = new Model({}, { url: c.url });

In my use case, I have a view updating on the collection's add event. I have another view to create the model. I do not want the collection view to update until the model is saved. Therefore, I cannot add the model to the collection until it is saved. The collection options seemed like a clean way to pass collection into the view with the model.

Feel free to close if I misunderstood the intent of this option. If not, I'll be happy to submit an update.

Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions