Backbone.js的 - ID VS idAttribute VS CID [英] Backbone.js - id vs idAttribute vs cid
问题描述
我一直在学习Backbone.js的几个星期,我觉得用配车型,路由器和收藏的意见。
我仍然有一些很大的差距:
-
什么是
ID
,CID
之间的连接,以及idAttribute
?他们如何相互影响? -
在究竟一个新的模式得到它的ID?是服务器负责分配的呢?我是否需要将其添加到模型(也许作为一个函数)的
默认
?也许addNewModel
函数应该这样做吗?
什么是
ID
,CID
之间的连接,以及idAttribute
?它们是如何相互影响?
无论是 CID 和 ID 的应该是唯一的ID为模型,并可以用来检索的一个模型的from 的集合。
这两者之间的区别是,在的cid
由Backbone.js的客户端分配的,是有用的,如果你没有一个实际的id,或者是因为该模型有尚未保存到服务器或者你甚至不将其保存到一个数据库(也许你正在使用的 localStorage的)。该 ID
属性应该是来自您的服务器模型的ID(即id为你的数据库是什么)。 idAttribute
告诉骨干其中的场,从您的服务器未来它应该用于更新 ID
属性,默认情况下,这是设置为ID,但因为它说,在文档如果服务器使用别的东西,你可以将其设置为(本例鉴于被设置为_id。
当究竟一个新的模式得到它的ID?是服务器负责分配的呢?我是否需要将其添加到模型(也许作为一个函数)的
默认
?也许addNewModel
函数应该这样做吗?
当保存到,你不应该需要手动设置的服务器,他们应该得到新的ID(根据 idattribute
),除非你需要更多的控制过程。
I've been studying Backbone.js for a few weeks, and I feel comfortable using views with models, routers, and collections.
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?
What is the connection between
id
,cid
, andidAttribute
? How do they affect each other?
Both the cid and id should be unique id's for the model, and can be used to retrieve a model from a collection.
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 documentation if your server uses something else you can set it to that (the example given is setting it to "_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?
They should get the new id's when saved to the server and you shouldn't need to set it manually (based on the idattribute
) unless you need more control over the process.
这篇关于Backbone.js的 - ID VS idAttribute VS CID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!