1

I am facing a problem which I am not aware how to resolve. Let me describe elaborately below:

I have a commonViewModel kendo class where event like save, cancel are written. I am facing problem with the save event of this class.

    save: function () {
    var that = this;
    var routeLanguage = "";
    that._showBackConfirmation(false);
    that.set("isFormSubmitted", true);
    console.log("form is valid, sending the save request!");


    if (vm.get("languageTabsVm.selectedLanguage")) {
        routeLanguage = "/" + vm.get("languageTabsVm.selectedLanguage");
    }
    else if (that.get("model.Languages") && that.get("model.Languages").length > 1) {
        that.get("model.Languages").forEach(function (lang) {
            if (lang.get("IsActive") === true) {
                //sätt cv-visning till det språk jag senast redigerade på detta item
                routeLanguage = "/" + lang.LanguageId;
            }
        });
    }


    //if i call the function _loadDefaultLanguageSelection here, it
    // works. because, the datasource is not synced yet. 


    //Make sure the datasource are syncing changes to the server (includes all crud)
    return that.dataSource.sync().fail(function (e) {

        //i need to do something here to be in the same language tab. But
       //as i am changing directly in to model, it is not possible. But 
       //saving directly to model is essential because that model is 
       //shared to other viewmodel for language tab synching purpose. 
        that.set("isFormSubmitted", false);
        console.log("form rejected");
    }).done(function () {
        if (that.get("isPersonaldetail")) {
            var name = that.get("model.Name");
            if (name.length > 12)
                name = name.substring(0, 11) + "...";
            $("#profileName").text(name);
        }
        that.set("isFormSubmitted", false);

        that.set("isSelected", false);

        // it is called from here right now. but it is failing because
        // model is updated but not synced in that function
        that._loadDefaultLanguageSelection();
        router.navigate(that.nextRoute + routeLanguage);
    });
},
_loadDefaultLanguageSelection: function () {
    var that = this;
    if (that.get("model.Languages") && that.get("model.Languages").length > 1) {
        that.get("model.Languages").forEach(function (lang) {
            if (!that.get("isPersonaldetail")) {
                lang.set("IsActive", lang.get("LanguageId") === vm.get("languageTabsVm.selectedLanguage"));
            }
        });

    }
},

So, my question is, how can i resolve this problem. one solution is i will have to sync twice. that is not nice. So, I am looking for efficient solution.

0 Answers0