簡體   English   中英

骨干網保存模型問題

[英]Backbone save model issues

我正在嘗試保存模型,並在成功后將其取消渲染:問題是,從成功之內,我無法引用此引用(即視圖),也無法引用this.model的變量isOk.status。 save(...)返回。 編碼:

save: function(e) {
    e.preventDefault();

    var isOk = this.model.save(null,
        {
            wait: true,

            success: function(model, response){
                console.log(response);
                console.log(response.status);

            },

            error: function(model, response){
                console.log("error");
                console.log($.parseJSON(response.responseText));
                $('#errorMessage').empty();
                $('#errorMessage').append($.parseJSON(response.responseText).error);
                $('#errorApproveModal').modal({
                    keyboard: true
                });
            }
        });
    console.log('logging isOk');
    console.log(isOk);
    //this one is working! It's on validate event
    if(!isOk){
        $('#errorMessage').empty();
        $('#errorMessage').append("Error: there was an error");
        $('#errorApproveModal').modal({
            keyboard: true
        });

        return false

    }
    console.log(isOk);
    **//both those checks are not working for some reason.**
    //
    if(isOk.status == 200 || isOk.statusText == "OK"){
        console.log('in is ok');
        this.remove();
    }

    return false;
}

順便說一下,視圖是:

App.Views.User = Backbone.View.extend({
      model: App.Models.User
      ,
      save: function...
});

有人可以幫忙嗎? 是否有比此方法更好的處理成功和錯誤的方法? 謝謝!! 羅伊

我不確定這是否是正確的方法,但是我總是只聲明一個從視圖函數中引用this變量的變量,然后成功使用它。 像這樣:

save: function(e) {

    // ADD THIS LINE
    var me = this;

    var isOk = this.model.save(null,
        {

    ....
            success: function(model, response){
                // USE me IN HERE
                me.render(); // e.g

            },
    ....
}

您也可以這樣做:

保存:功能(e){

var isOk = this.model.save(null,
    {

....
        success: function(model, response,options){
            // USE me IN HERE
            this.options.me.render(); // e.g

        },
        //ADD me this 
        me : this
....
}

使用這些選項,您可以執行所有參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM