问题描述
很抱歉,如果这是一个有点令人费解......我仍然在学习Backbone.js的...
Sorry if this is a bit convoluted... I am still learning Backbone.js...
什么是负载和放大器的正确方法;保存有在自身子模型骨干机型? (应该我甚至是具有子模型?)
What is the proper way to load & save Backbone models that have sub-models within themselves? (And should I even be having sub-models?)
例如,(原谅的CoffeeScript),如果我有这样的:
For example, (pardon the coffeescript), if I have something like:
class Address extends Backbone.Model
urlRoot: '/api/v1/address/'
url: -> return @urlRoot+@id+'/?format=json'
defaults: {'city': '', 'state': ''}
class Person extends Backbone.Model
urlRoot: '/api/v1/person/'
url: -> return @urlRoot+@id+'/?format=json'
defaults: { name: 'Anon', address: new Address }
... and then I do this ...
dude = new Person
dude.set('id',101)
dude.fetch()
// Response returns {name: 'The Dude', address: '/api/v1/address/1998/'}
// And now, dude.get('address') is '/api/v1/address/1998' and no Address object
where = new Address
where.set('id',1998)
where.fetch()
// Response returns {city: 'Venice', state; 'CA'}
我要什么是说dude.fetch(),并为它获得两个家伙和他的地址,当我打电话Backbone.sync('更新',伙计),我想以节省的家伙和他的地址。怎么样?
What I want is to say dude.fetch() and for it to get both the dude and his address, and when I call Backbone.sync('update',dude), I want to save both the dude and his address. How?
在后台,我使用tastypie来构建我的一些SQLAlchemy的表(而不是Django的ORM)的API,所以我有我的个人表和地址表的资源:
On the backend, I am using tastypie to construct my api for some SQLAlchemy tables (not Django's ORM), and so I have a resource for my Person table and Address table:
class AddressResource(SQLAlchemyResource):
class Meta:
resource_name = 'address'
object_class = AddressSQLAlchemyORMClass
class PersonResource(SQLAlchemyResource):
address = ForeignKey(AddressResource, 'address')
class Meta:
resource_name = 'person'
object_class = PersonSQLAlchemyORMClass
Tastypie的函数创建一个返回映射的的网址所讨论的地址。
Tastypie's ForeignKey function creates a mapping that returns the URL to the address in question.
我试图重载Dude.parse()函数来调用获取的地址(),但它不工作了,感觉错了,它提出了各种各样的问题:
I tried overloading the Dude.parse() function to call fetch for the Address(), but it wasn't working and it felt wrong, and it raised all sorts of questions:
- 我应该修改我tastypie响应包括地址作为一个嵌套的对象?
- 如果我更改为一个嵌套的对象,我应该使用,如在提问Backbone-Relational相关模型没有被创建,或者是大材小用?
- 我应该或取( )功能或创建我自己的backbone.Sync()获得响应,然后做手工?
- 既然是一对的,我应该不是只是一个模型,而不是一个子模型,穿梭在一个请求一起发送的信息回来?
- Should I be modifying my tastypie response to include the Address as a nested object?
- If I change to a nested object, should I be use backbone-relational, as in the question Backbone-Relational related models not being created, or is that overkill?
- Should I be overloading the parse() or fetch() function or creating my own backbone.Sync() to get the response and then do this manually?
- Since it is one-to-one, should I instead just have one model, instead of a sub-model, and send the information back forth together in one request?
是否有这样做的标准方法?
Is there a standard way of doing this?
推荐答案
通过Tastypie,可以响应改变是一个嵌套的对象,而不是一个链接,通过在指定的全= TRUE ForeignKey的定义:
With Tastypie, you can change the response to being a nested object, instead of a link, by specifying full=True in the ForeignKey definition:
class PersonResource(SQLAlchemyResource):
address = ForeignKey(AddressResource, 'address', full=True)
这将返回地址对象与人一起。
This returns the address object along with the person.
接下来,我还是不知道的如果这是最好的办法的,但我动了我的分型出的属性和重载解析()来设置它,和update()保存它,如:
Next, I still don't know if this is the best way, but I moved my sub-model out of the attributes, and overloaded parse() to set it, and update() to save it, like:
class Person extends Backbone.Model
address: new Address
urlRoot: '/api/v1/person/'
url: -> return @urlRoot+@id+'/?format=json'
defaults: { name: 'Anon'}
parse: (response) ->
addressResp = response.address || {}
addressAttr = @address.parse(addressResp)
@address.set(addressAttr)
return response
update: (options) ->
@set('address',@address.toJSON())
Backbone.sync 'update', @, options
在这个例子中,我可以重新添加地址的属性和设置管理/获取,但随后在自己的环境中,我创建了一个备用的toJSON函数的JSON张贴到服务器,couldn 找不到一个很好的方式来设置JSON对象,而不从集合改变它的JSON响应。
In this example, I could add the address back to the attributes and manage it with set/get, but then in my own environment, I've created an alternate toJSON function for the json to post to the server, and couldn't find a good way to set the json to the object without changing it from a collection to a json response.
这篇关于如何加载子模型与Backbone.js的一个外键关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!