我在模型中编写了自己的sync方法,然后使用回调调用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(选项):
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