Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 03:50 PM   PM User | #1
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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());
__________________

nikos101 is offline   Reply With Quote
Old 02-02-2013, 11:43 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
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 …
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-02-2013, 12:49 PM   PM User | #3
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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>
__________________

nikos101 is offline   Reply With Quote
Old 02-02-2013, 12:54 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
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).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-02-2013, 12:55 PM   PM User | #5
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
sorry that gets compiled to false serverside, its Razor asp mvc syntax
__________________

nikos101 is offline   Reply With Quote
Old 02-02-2013, 12:58 PM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
well, the question about the ko variable remains.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-02-2013, 01:30 PM   PM User | #7
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
sorry I don't get what you mean by ko var.

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

nikos101 is offline   Reply With Quote
Old 02-02-2013, 01:37 PM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
aah, now it makes way more sense.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-02-2013, 01:53 PM   PM User | #9
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
1000sandth post!"!!!!!!
__________________

nikos101 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:46 AM.


Advertisement
Log in to turn off these ads.