我是Backbone和MongoDB的新手。我遇到了摧毁模型(由MongoDB支持)的问题。我相信它与事实有关,MongoDB“文档”使用异常_id
属性,并且默认情况下与Backbone没有连接。我试过设置idAttribute: '_id'
,这似乎不能解决问题。骨干model.destroy与MongoDB持久
样品蒙戈文档看起来像:
{
_id: Object
$oid: "527303e82f3504ba5bf4b21f"
__proto__: Object
feeling: "ok"
location: "california"
name: "Kevin"
}
models.coffee
class Models.Campaign extends Backbone.Model
urlRoot: "http://localhost:4567/api/campaigns"
# setting idAttribute: '_id' causes issues rendering the Backbone collection
controller.coffee
...
model.destroy() #=> only removes the item from the dom but does not make a DELETE request
我能否将id
传递给destroy()函数?
有没有人建议如何正确连接主干与MongoDB,而不必monkeypatch大量的内置Backbone功能?
'urlRoot:'/ api/campaigns''工作吗?结合'idAttribute:'_id''当然。 –
不完全...设置'idAttribute:'id''会中断Backbone集合的加载,但会尝试使用错误的参数进行AJAX删除请求(我真的需要'_id。$ oid',如上所示) – pruett
JSON来自服务器看起来像'{_id:...,$ oid:...}'?据推测,服务器希望看到'DESTROY/api/campaigns/527303e82f3504ba5bf4b21f'杀死(服务器端)模型,对吧? –