Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts

    Question ko.computed does not update

    How come the followButtonText doesn't update then isFollowing is changed?
    Code:
         function AppViewModel() {
    
                    this.toggleIsFollowing = function () {
                        this.isFollowing = !this.isFollowing
                    };
    
                    this.isFollowing = ko.observable(false);
                    this.followButtonText = ko.computed(function () {
                        return this.isFollowing ? "Unfollow" : "Follow";
                    });
                }
    
                ko.applyBindings(new AppViewModel());


  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,248
    Thanks
    12
    Thanked 340 Times in 336 Posts
    there is not enough information (esp. about the ko instance) to say anything definite. it might be a scope issue, it might be that the methods do not do what you expect …
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    this is all the code for KO in this app

    Code:
    <script type="text/javascript">
            $(document).ready(function () {
    
    
                function AppViewModel() {
    
                    this.toggleIsFollowing = function () {
                        this.isFollowing = !this.isFollowing;
                        follow();
                    };
    
                    this.isFollowing = ko.observable(false);
                    this.followButtonText = ko.computed(function () {
                        return this.isFollowing ? "Unfollow" : "Follow";
                    });
                }
    
                ko.applyBindings(new AppViewModel());
    
            });
        </script>
    and HTML element using that text:

    Code:
     <button class="radius secondary button" data-bind="text: followButtonText,click: toggleIsFollowing"></button>


  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,248
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Code:
    this.isFollowing = ko.observable(@Model.IsFollowing.ToString().ToLower());
    which language is that? and what part thereof is JS?

    if the posted code is everything and ko is supposedly a JS object, you will get a TypeError (ko is null or not an object).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    sorry that gets compiled to false serverside, its Razor asp mvc syntax


  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,248
    Thanks
    12
    Thanked 340 Times in 336 Posts
    well, the question about the ko variable remains.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    sorry I don't get what you mean by ko var.

    I just use it like this site:
    http://learn.knockoutjs.com/#/?tutorial=intro


  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,248
    Thanks
    12
    Thanked 340 Times in 336 Posts
    aah, now it makes way more sense.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    1000sandth post!"!!!!!!



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •