Javascript Backbone.js - id vs idAttribute vs cid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12169822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone.js - id vs idAttribute vs cid
提问by Daniel
I've been studying Backbone.js for a few weeks, and I feel comfortable using views with models, routers, and collections.
我已经研究 Backbone.js 几个星期了,我觉得在模型、路由器和集合中使用视图很舒服。
I still have some big gaps:
我仍然有一些很大的差距:
What is the connection between
id
,cid
, andidAttribute
? How do they affect each other?When exactly does a new model get its ID? Is the server responsible for assigning it? Do I need to add it to the
defaults
of the model (maybe as a function)? Maybe theaddNewModel
function should do that?
什么是之间的连接
id
,cid
和idAttribute
?它们如何相互影响?新模型究竟何时获得其 ID?服务器负责分配吗?我是否需要将它添加到
defaults
模型中(也许作为一个函数)?也许addNewModel
函数应该这样做?
回答by Hyman
What is the connection between
id
,cid
, andidAttribute
? How do they affect each other?
什么是之间的连接
id
,cid
和idAttribute
?它们如何相互影响?
Both the cidand idshould be unique id's for the model, and can be used to retrievea model froma collection.
无论是CID和ID应该是唯一的ID为模型,并可以用来检索模型从一个集合。
The difference between the two is that the cid
is assigned by backbone.js client side and is useful if you don't have an actual id, either because the model hasn't been saved yet to the server or perhaps you aren't even saving it to a db (maybe you're using localStorage). The id
attribute should be the id of the model that comes from your server (that is what the id is in your db). idAttribute
tells backbone which "field" coming from your server it should use to update the id
attribute, by default this is set to "id" but as it says in the documentationif your server uses something else you can set it to that (the example given is setting it to "_id".
两者之间的区别在于,cid
由backbone.js 客户端分配,如果您没有实际的 id,则很有用,因为模型尚未保存到服务器,或者您甚至没有保存它到一个数据库(也许你正在使用localStorage)。该id
属性应该是来自您的服务器的模型的 id(即 id 在您的数据库中)。idAttribute
告诉主干它应该使用来自您的服务器的哪个“字段”来更新id
属性,默认情况下将其设置为“id”,但正如文档中所说,如果您的服务器使用其他内容,您可以将其设置为(给出的示例)将其设置为“_id”。
When exactly does a new model get its ID? Is the server responsible for assigning it? Do I need to add it to the
defaults
of the model (maybe as a function)? Maybe theaddNewModel
function should do that?
新模型究竟何时获得其 ID?服务器负责分配吗?我是否需要将它添加到
defaults
模型中(也许作为一个函数)?也许addNewModel
函数应该这样做?
They should get the new id's when saved to the serverand you shouldn't need to set it manually (based on the idattribute
) unless you need more control over the process.
他们应该在保存到服务器时获得新的 id,idattribute
除非您需要对过程进行更多控制,否则您不需要手动设置它(基于)。
回答by 3coins
id - id that might be manually set when the model is created, or is populated when model has been saved on the server (see "idAttribute" at the bottom to see the connection). This is the id that is sent to the server when model is loaded or updated from server e.g., for a model Person this call will be made if id is 123, "/person/123"
id - 可以在创建模型时手动设置的 id,或者在模型已保存在服务器上时填充的 id(请参阅底部的“idAttribute”以查看连接)。这是在从服务器加载或更新模型时发送到服务器的 id,例如,对于模型 Person,如果 id 为 123,则将进行此调用,“/person/123”
cid - unique id set my backbone model for internal use
cid - 唯一 id 设置我的主干模型供内部使用
idAttribute - this decides which property will act as the unique id (default is "id") when the model has been saved on the server e.g., a model's unique key on the server might be defined by "personId", so when fetch is called model will map the server response from "personId" to id in the backbone model.
idAttribute - 这决定了当模型已保存在服务器上时哪个属性将充当唯一 id(默认为“id”),例如,服务器上模型的唯一键可能由“personId”定义,因此当调用 fetch 时模型会将服务器响应从“personId”映射到主干模型中的 id。
回答by Ryan Lv
id
is server model id, cid
is client id.
id
是服务器模型ID,cid
是客户端ID。
- server model: such as Rails Model
- client model: backbone model
- 服务器模型:如 Rails 模型
- 客户端模型:骨干模型
回答by Dhaval Rajpara
The id
property on a model is automatically assigned based on the id
set in the model's attributes hash. Ideally, this is the ID that you receive from the rest API for the resource that you are querying. On the other hand, cid
is an ID temporarily assigned to each model and is useful until an actual ID is determined for the object. For example, a model pushed to a collection that has not yet been persisted can be addressed using cid
, until it is saved in the database and an actual ID is generated for it.
id
模型上的属性是根据id
模型属性散列中的设置自动分配的。理想情况下,这是您从正在查询的资源的其余 API 收到的 ID。另一方面,cid
是临时分配给每个模型的 ID,在确定对象的实际 ID 之前很有用。例如,推送到尚未持久化的集合的模型可以使用cid
,处理,直到将其保存在数据库中并为其生成实际 ID。