我有一个使用 Backbone.js 框架的模型,当用户在模型视图中单击链接时,我试图销毁它。该视图类似于以下内容(这是伪代码,因为它是使用CoffeeScript 实现的,可以在问题底部找到)。
有趣的是,当我在错误回调函数中记录
我做错了什么?
这是服务器的响应:
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
链接时,尽管我的服务器记录了关联数据库记录的删除成功并返回200响应,但我总是在控制台中看到Error
。当我刷新页面(导致集合从数据库重新渲染)后,我删除的模型将被删除。有趣的是,当我在错误回调函数中记录
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
这是与destroy交互的服务器动作(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 的人的实际 Backbone View 实现:
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
head: ok
。我将我的JSON响应更改为render json:@listing_save
,其中@listing_save
是我刚刚销毁的记录,并且它注册了一个成功。 - David Tuite.json
吗?由于你在服务器端进行了检查(respond_to do |format| ... end
),你可能没有发送正确的head: ok
响应... - Tricote