骨干model.destroy()调用错误回调函数,即使它正常工作? [英] Backbone model.destroy() invoking error callback function even when it works fine?

查看:158
本文介绍了骨干model.destroy()调用错误回调函数,即使它正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Backbone.js的模型在模型的视图。的观点是这样的(伪code,因为它在实施的CoffeeScript 可在底部找到这个问题)。

  VAR window.ListingSaveView = Backbone.View.extend({
  事件:{
    点击a.delete':'的onDestroy
  },  的onDestroy:函数(事件){
    。事件preventDefault();
    this.model.destroy({
      成功:函数(模型,响应){
        CONSOLE.LOG成功;
      },
      错误:功能(模型,响应){
        CONSOLE.LOG错误;
      }
    });
  }
});

当我点击删除浏览器链接,我总是错误登录到控制台,即使我的服务器记录相关联的数据库记录的成功销毁和返回200响应。当我刷新页面(从DB造成集重新渲染)我删除模式将不复存在。

一个有趣的是,当我登录错误回调的响应,它有状态code 200 表示成功,但它也报告状态文本:parseerror是什么意思。没有错误在我的服务器日志。

我在做什么错了?

这是来自服务器的响应:

 对象
    中止:功能(状态文本){
    总是:功能(){
    完成:功能(){
    完成:功能(){
    错误:函数(){
    失败:功能(){
    getAllResponseHeaders:功能(){
    getResponseHeader:功能(键){
    isRejected:功能(){
    isResolved:功能(){
    overrideMimeType:功能(类型){
    管:功能(fnDone,fnFail){
    承诺:函数(OBJ){
    readyState的:4
    responseText的:
    setRequestHeader:函数(名称,值){
    状态:200
    状态code:功能(图){
    状态文本:parsererror
    成功:函数(){
    那么:功能(doneCallbacks,failCallbacks){
    __proto__:对象

下面是破坏与(Ruby on Rails的)

交互的服务器操作

 #删除/team/listing_saves/1.json
  DEF破坏
    @save = current_user.team.listing_saves.find(PARAMS [:ID])
    @ save.destroy
    做的respond_to |格式|
      format.json {头:OK】
    结束
  结束

这是骨干观为人们的实际CoffeeScript的执行谁preFER会这样:

 类MoveOutOrg.Views.ListingSaveView扩展Backbone.View
  标签名:礼
  产品类别:'listing_save
  模板:JST ['骨干/模板/ listing_save']
  事件:
    点击a.delete_saved':'的onDestroy  初始化: - >
    @ model.bind'变',this.render
  渲染:=>
    renderedContent = @Template(@ model.toJSON())
    $(@ EL)的.html(renderedContent)
    这个
  的onDestroy:(事件) - GT;
    事件。preventDefault()#停止哈希被添加到URL
    的console.log挂牌毁
    @ model.destroy
      成功:(模型,响应) - GT;
        的console.log成功
        的console.log模型
        响应的console.log      错误:(模型,响应) - GT;
        的console.log错误
        的console.log型号#这是ListingSave模型
        响应的console.log


解决方案

@大卫Tuite评论:


  。

好吧,我理解了它似乎骨干预计JSON响应是被破坏的纪录的JSON序列但是,Rails的控制器,发电机只能返回头:默认情况下,确定我改变了我的JSON响应是渲染JSON:@listing_save哪里@listing_save是我刚刚摧毁了记录,并注册成功


仅供参考 - 当你做一个毁灭,你并不需要返回完整的JSON的破坏模式。你可以返回一个空的JSON哈希,它会工作得很好。你需要返回的JSON为模型的唯一时间是在一个保存/更新。

I have a Backbone.js model that I'm trying to destroy when the user clicks a link in the model's view. The view is something like this (pseudocode because it's implemented in CoffeeScript which can be found at the bottom of the question).

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";
      }
    });
  }
});

When I click the delete link in the browser, I always get Error logged to the console even though my server records successful destruction of the associated database record and returns a 200 response. When I refresh the page (causing the collection to re-render from the DB) the model I deleted will be gone.

One interesting this is that when I log the response in the error callback, it has statuscode 200 indicating success but it also reports statusText: "parseerror" whatever that means. There is no error in my server logs.

What am I doing wrong?

This is the response from the server:

  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

Here is the server action that destroy interacts with (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

And here is the actual CoffeeScript implementation of the Backbone View for people who prefer it like that:

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

解决方案

@David Tuite comment:

"Ok I figured it out. It seems that Backbone expects the JSON response to be a JSON serialization of the record that was destroyed. However, Rails controller generators only return head :ok by default. I changed my JSON response to be render json: @listing_save where @listing_save is the record I just destroyed and it registers a success."

FYI - when you're doing a destroy, you don't need to return the full json for the destroyed model. you can return an empty json hash and it will work just fine. the only time you need to return the json for the model is on a save / update.

这篇关于骨干model.destroy()调用错误回调函数,即使它正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆