Backbone.js - id vs idAttribute vs cid [英] Backbone.js - id vs idAttribute vs cid

查看:15
本文介绍了Backbone.js - id vs idAttribute vs cid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究 Backbone.js 几个星期了,我觉得在模型、路由器和集合中使用视图很舒服.

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:

  1. idcididAttribute 之间有什么联系?它们如何相互影响?

  1. What is the connection between id, cid, and idAttribute? How do they affect each other?

一个新模型究竟什么时候获得它的 ID?服务器负责分配吗?我是否需要将它添加到模型的 defaults 中(也许作为一个函数)?也许 addNewModel 函数应该这样做?

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 the addNewModel function should do that?

推荐答案

idcididAttribute 之间有什么联系?它们如何相互影响?

What is the connection between id, cid, and idAttribute? How do they affect each other?

cidid 应该是模型的唯一 ID,可用于检索模型 来自 一个集合.

Both the cid and id should be unique id's for the model, and can be used to retrieve a model from a collection.

两者的区别在于 cid 是由backbone.js 客户端分配的,如果你没有实际的 id,或者因为模型还没有保存,这很有用到服务器,或者您甚至没有将其保存到数据库(也许您正在使用 localStorage).id 属性应该是来自您的服务器的模型的 id(即 id 在您的数据库中).idAttribute 告诉主干它应该使用来自您的服务器的哪个字段"来更新 id 属性,默认情况下它被设置为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 documentation if your server uses something else you can set it to that (the example given is setting it to "_id".

新模型究竟何时获得其 ID?服务器负责分配吗?我是否需要将它添加到模型的 defaults 中(也许作为一个函数)?也许 addNewModel 函数应该这样做?

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 the addNewModel function should do that?

当保存到 服务器 时,他们应该会获得新的 ID,您不需要手动设置它(基于 idattribute),除非您需要对流程进行更多控制.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆