モデルに独自の同期メソッド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