我有一個Backbone.js模型,我試圖在用戶點擊模型視圖中的鏈接時銷燬。該視圖是這樣的(僞代碼,因爲它在CoffeeScript中實現,可以在問題的底部找到)。Backbone model.destroy()調用錯誤回調函數,即使它工作正常嗎?
var window.ListingSaveView = Backbone.View.extend({
events: {
'click a.delete': 'onDestroy'
},
onDestroy: function(event){
event.preventDefault();
this.model.destroy({
success: function(model, response){
console.log "Success";
},
error: function(model, response){
console.log "Error";
}
});
}
});
當我點擊瀏覽器中的delete
鏈接,我總是得到Error
登錄到控制檯,即使我的服務器記錄相關的數據庫記錄的成功銷燬,並返回200響應。當我刷新頁面(導致集合從數據庫重新渲染)時,我刪除的模型將消失。
一個有趣的是,當我在錯誤回調中登錄response
時,它的狀態碼200
表示成功,但它也報告statusText: "parseerror"
無論如何。我的服務器日誌中沒有錯誤。
我在做什麼錯?
這是來自服務器的響應:
Object
abort: function (statusText) {
always: function() {
complete: function() {
done: function() {
error: function() {
fail: function() {
getAllResponseHeaders: function() {
getResponseHeader: function (key) {
isRejected: function() {
isResolved: function() {
overrideMimeType: function (type) {
pipe: function (fnDone, fnFail) {
promise: function (obj) {
readyState: 4
responseText: " "
setRequestHeader: function (name, value) {
status: 200
statusCode: function (map) {
statusText: "parsererror"
success: function() {
then: function (doneCallbacks, failCallbacks) {
__proto__: Object
這裏是服務器的行動,摧毀與(Ruby on Rails的)
# DELETE /team/listing_saves/1.json
def destroy
@save = current_user.team.listing_saves.find(params[:id])
@save.destroy
respond_to do |format|
format.json { head :ok }
end
end
相互作用,這裏是實際的CoffeeScript實現骨幹查看喜歡的人:
class MoveOutOrg.Views.ListingSaveView extends Backbone.View
tagName: 'li'
className: 'listing_save'
template: JST['backbone/templates/listing_save']
events:
'click a.delete_saved': 'onDestroy'
initialize: ->
@model.bind 'change', this.render
render: =>
renderedContent = @template(@model.toJSON())
$(@el).html(renderedContent)
this
onDestroy: (event) ->
event.preventDefault() # stop the hash being added to the URL
console.log "Listing Destroyed"
@model.destroy
success: (model, response)->
console.log "Success"
console.log model
console.log response
error: (model, response) ->
console.log "Error"
console.log model # this is the ListingSave model
console.log response
你可以粘貼任何服務器發送回來的東西(使用螢火蟲或其他東西來打開響應),如果有的話? – Stephen
編輯我的問題,包括回覆 –
哦。那麼,直言不諱,這不是一個有效的迴應。這很可能是在聽一個JSON響應..不管怎樣,你都沒有給它一個。我不記得我們在Rails中使用什麼,但它就像to_json,而不僅僅是'json'(我不是一個紅寶石專家,或者甚至是新手......所以我無法幫助過去) – Stephen