我是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'殺死(服務器端)模型,對吧? –