2014-02-20 34 views
0

關於同一個錯誤有很多問題,但這個是不同的,也很奇怪。Backbone.js同步:必須指定一個「url」屬性或函數

這裏是正在生成的錯誤代碼:

console.log(this.sources.url); 
this.sources.sync({ 
    success: function (collection, response, options) { 
     App.debug('Synchronized media sources.'); 
    }, 
    error: function (collection, response, options) { 
     App.debug('Unable to synchronize media sources.') 
     App.debug(response); 
    } 
}); 

this.sources是一個集合。輸出是:

/api/sources 
routes.js:40 Uncaught Error: A "url" property or function must be specified 

所以指定一個url。有任何想法嗎?

+0

不過不知道爲什麼這是怎麼回事,但我找到了一個解決方法。我使用同步而不是抓取,因爲我想聽同步事件。然後我意識到提取也會觸發同步事件,並且切換到提取可以修復問題。 – elplatt

回答

0

Sync需要一個方法('create','read','update'...)通過省略一個方法,您可能會混淆這些參數。

sync: function() { 
     return Backbone.sync.apply(this, arguments); 
    }, 


    Backbone.sync = function(method, model, options) { 
... 

它希望像

this.sources.sync('create',{ 
    success: function (collection, response, options) { 
     App.debug('Synchronized media sources.'); 
    }, 
    error: function (collection, response, options) { 
     App.debug('Unable to synchronize media sources.') 
     App.debug(response); 
    } 
}); 
+1

這是不正確的。你不能以這種方式保存「集合」;它不是RESTful。但是你總是可以保存一個模型,就像這樣:var collectionWrapperModel = new(Backbone.Model.extend({url:'api/endpoint'}))({update:this.collection.models}); collectionWrapperModel.save();'請注意,服務器然後會收到一個名爲'update'的屬性的POST或PUT消息,然後需要進行適當的處​​理。我不會發布這個問題作爲答案,因爲提問者實際上是在試圖「取回」他的收藏。 – Lambart

相關問題