Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:58:47+00:00 2026-06-03T05:58:47+00:00

followed the docs, tried my own and bump into some problem. initializeViewModel = function(){

  • 0

followed the docs, tried my own and bump into some problem.

initializeViewModel = function(){

    var listing_model = {
        sale_rent: ko.observable( jQuery('#id_sale_rent')),
        property_type: ko.observable( jQuery('#id_property_type').val()),
        address: ko.observable( jQuery('#id_address')),
        is_condo: ko.computed(function(){
            return this.property_type() == 'condominium';
        }, this)
    };



    listing_model.district = ko.computed(function(){

        return this.district() || this.property_type();
    }, listing_model);

    return listing_model;
}

The statement return this.property_type() == 'condominium'; causes an exception object <object> has no method property_type(). I think this might be a scoping issue, but this seems to be referring to the right instance here. Can someone please point out my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:58:49+00:00Added an answer on June 3, 2026 at 5:58 am

    The cleanest solution is to use an anonymous function (to create a closure) rather than a plain object:

    initializeViewModel = function(){
        var listing_model = new function() {
            // Close-in a reference to this object
            var self = this;
    
            self.sale_rent = ko.observable( jQuery('#id_sale_rent') );
            self.property_type = ko.observable( jQuery('#id_property_type').val() );
            self.address = ko.observable( jQuery('#id_address') );
    
            self.is_condo = ko.computed(function() {
                return (self.property_type() == 'condominium');
            });
        }();
    
        // ...
    

    Otherwise, “this” inside the function (that defines the computed) refers to whatever you’re passing as the second parameter to ko.computed() – the value of the “this” there is the current context that “initializeViewModel” is executed in, so if you’re calling that function as usual (i.e. initializeViewModel()), “this” will just be a reference to the global object and not to “listing_model” (as expected/intended).

    The example in the manual differs from you’re code: you’re creating a plain object right away while in the manual everything is wrapped in a function. Calling that function with the “new” keyword creates a new object and sets the context (“this”) to this object. That’s why their code works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/ and I created a flatpage using my own template in http://localhost:8000/about-me/
I followed the docs on cap + nginx + unicorn but have some problems
First go at starting my own service in Delphi 7. Followed the docs and
I am trying to integrate GWT with SEAM. i followed the Docs and tried
I have followed numerous screen casts as well as trawled the backbone docs extensively
I'm using this dialog: http://docs.jquery.com/UI/Dialog To open dialog I do it this way: $('a.openModal').live(click,
I am trying to setup TurboMail 3 with Pylons 1.0 Followed the docs here
I have followed the Poll tutorial on http://docs.djangoproject.com . I would like to know
I'm working on getting Expresso set up and some tests running. I followed along
I'm having issues with threads not executing. I followed the docs on how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.