微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Backbone – Collection.add()/ Collection.create()之间的区别?

我对两者之间的差异感到困惑.似乎Collection.create()(触发添加和同步事件)可以看作Collection.add()(fires add)和Model.save()(触发同步)的组合?

以上评估是否正确?我错过了什么?

解决方法

那就对了.这是一种捷径方法. 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.

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.

第二个描述更准确一点,因为只有传递{wait:true}才会在保存模型时出现错误时不向模板中添加模型.

原文地址:https://www.jb51.cc/js/150607.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐