1

我在模型中编写了自己的同步方法,然后model.save()用回调调用成功和错误不起作用。我究竟做错了什么?

我认为删除文件的处理程序:

dropHandler: function( e ) {
    e.stopPropagation()
    e.preventDefault()
    var files = e.dataTransfer.files
    _.each( files, function( file ) {
      this.tmpRetusjId++
      this.collection.add({
        tmpId: this.tempRetusjId,
        file: file
      })
    }, this )
    return false
  },

在这里,我收听集合的“添加”,如果成功,我想为新文件呈现缩略图:

addFile: function( model, index ) {
    model.save({}, {
      success: _.bind( function( m, xhr ) {
        console.log("success")
      }, this ),
      error: _.bind( function( m, xhr ) {
        console.log("noes!")
      }, this )
    })
  },

我自己的同步方法:

sync: function( method, model, options ) {
    // options.error() - this works
    // options.success() - this does not work
    // return
    if ( method == "create" ) {
      if ( !model.has("file") || !model.has("tmpId") ) {
        return
      }
      var form = new FormData()
      form.append( 'file', model.get("file") )
      form.append( 'id', bootstrap.bestilling.id )
      form.append( 'tmpId', model.get("tmpId") )
      $.ajax({
        type: 'POST',
        url: 'someurl',
        data: form,
        dataType: 'json',
        contentType: false,
        cache: false,
        processData: false,
        success: function( file ) {
          if ( "tmpId" in file && 
            file.tmpId == model.get("tmpId") ) {
            for ( var i in file ) {
              model.set( i, file[i] )
            }
            model.set( "id", file.foto_fil_id )
            if ( "success" in options ) {
              options.success( model, file )
            }
          }
        },
        error: function( xhr ) {
          if ( "error" in options ) {
            options.error( model, xhr )
          }
          //self.addErrorFile( xhr.responseText )
        }
      })
    } else {
      return Backbone.sync( method, model, options )
    }
  }

错误回调将记录到控制台,但成功给了我这个:

Uncaught TypeError: Cannot set property 'attributes' of undefined backbone-min.js:16

现在,如果我与模型同步 console.log(options):

error: function () { [native code] }
parse: true
success: function (a,b,c){a.attributes=g;var k=a.parse(b,c);c.wait&&(k=f.extend(d||{},k));if(f.isObject(k)&&!a.set(k,c))return!1;e&&e(a,b,c)}
validate: true
4

0 回答 0