5

コレクションに新しいモデルを作成するための次のコードがあります。基盤となるデータストアはリモートAPIです。

        var postCreationStatus = this.model.create(newPostModel, {
            wait : true     // waits for server to respond with 200 before adding newly created model to collection
        }, {
            success : function(resp){
                console.log('success callback');
                console.log(resp);
            },
            error : function(err) {
                console.log('error callback');
                console.log(err);
            }
        });

新しいモデルが作成され、データベースからこれを確認できますが、成功コールバックもエラーコールバックも呼び出されません。

作成が完了したら、ユーザーをリダイレクトしたいと思います。リダイレクトするとAJAXリクエストが途中で強制終了されるため、成功コールバックを使用することが重要です。

サーバーは、JSON応答{ id : 11 }とHTTPステータスで応答します200 OK

4

1 に答える 1

6

バックボーンコードを調べてみると、create()関数の呼び出しが正しくないことがわかりました。成功とエラーのコールバックは、3番目の引数としてではなく、2番目の引数として渡されるオブジェクト内にある必要がありました。変更され、機能するスニペットは次のとおりです。

var postCreationStatus = this.model.create(newPostModel, {
    wait : true,    // waits for server to respond with 200 before adding newly created model to collection

    success : function(resp){
        console.log('success callback');
        console.log(resp);
        that.redirectHomePage();
    },
    error : function(err) {
        console.log('error callback');
        // this error message for dev only
        alert('There was an error. See console for details');
        console.log(err);
    }
});
于 2012-11-28T07:45:20.767 に答える