最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone - Difference between Collection.add()Collection.create()? - Stack Overflow

programmeradmin0浏览0评论

I am rather confused by the differences between the two. It seems like Collection.create() (fires add and sync events) can be seen as the bination of Collection.add() (fires add) and Model.save() (fires sync)?

Is the above assessment correct? What am I missing?

I am rather confused by the differences between the two. It seems like Collection.create() (fires add and sync events) can be seen as the bination of Collection.add() (fires add) and Model.save() (fires sync)?

Is the above assessment correct? What am I missing?

Share Improve this question asked Sep 2, 2012 at 9:01 gskleegsklee 4,9444 gold badges41 silver badges56 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

That's right. It's a shortcut method. Documentation states:

create collection.create(attributes, [options])

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.

And annotated source code:

Create a new instance of a model in this collection. Add the model to the collection immediately, unless wait: true is passed, in which case we wait for the server to agree.

This second description is a little bit more accurate as only passing {wait:true} has the effect of not adding a model to the collection in case of errors upon saving the model.

发布评论

评论列表(0)

  1. 暂无评论