Q:

Button not hitting if statement in Knockout js

I have the following button
    <div class="pull-right" data-bind="tooltip: { title: DashboardEdit, placement: 'top' }">
    <button class="btn actionButtonIcon" id="DashboardEdit" data-bind="click: changeButtonText">
        <figure>
            <img src="../../../Images/NotesPink.png" />
            <figcaption data-bind="text: $data.ProcurementbuttonText() ? 'Save': 'Edit'"></figcaption>
        </figure>
    </button>
</div>

It changes text when clicked, now what i am trying to achieve is when the button text is showing Save then it should execute a function, so i tried this: 
            self.ProcurementbuttonText = ko.observable(false);

        self.changeButtonText = function(){
            self.ProcurementbuttonText(!self.ProcurementbuttonText())
            if (self.ProcurementbuttonText() === 'Save'){
            var data = {
                'ScorecardId':ko.observable(localStorage.getItem('scorecardId'))(),
                'DashboardConfig':ko.observable(localStorage.getItem('ElementDataWidget'))()
            };
            PreferentialProcurementDashboardApi.Save(data);           
        }
    }

so when ProcurementButtonText = save then it should do all that which is in the if statement, but i see its never hitting that if statement
What am i doing wrong?

A:

I think the problem is with what you are expecting in self.ProcurementbuttonText() because you set it up with a boolean like so - self.ProcurementbuttonText(false); and then toggle it self.ProcurementbuttonText(!self.ProcurementbuttonText()) in the changeButtonText function.  the next line you are checking to see if it equals 'Save' which it will never equal to.  it will only be true or false.
so this check just needs to be 
if (self.ProcurementbuttonText()){
    var data = {
        'ScorecardId':ko.observable(localStorage.getItem('scorecardId'))(),
        'DashboardConfig':ko.observable(localStorage.getItem('ElementDataWidget'))()
    };
    PreferentialProcurementDashboardApi.Save(data);           
}

I would recommend that you look at how you are naming your variables and what they are used for because self.ProcurementbuttonText is misrepresenting what it actually contains.  At the moment it contains the view/edit state of this record and not the text of the procurement button. 

