Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How do I add a computed property via the prototype?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
StrandedPirate  
View profile  
 More options Apr 23 2012, 5:28 pm
From: StrandedPirate <joey_brads...@strandedpirate.com>
Date: Mon, 23 Apr 2012 14:28:31 -0700 (PDT)
Local: Mon, Apr 23 2012 5:28 pm
Subject: How do I add a computed property via the prototype?
I've got a model that I want to later extend with a computed variable
but it is throwing errors.

Here is my model:

function SiteModel(rootUrl, data) {
    var self = this;
    self.rootUrl = rootUrl;
    self.DomainName = ko.observable(data.DomainName);
    self.IsSubDomain = ko.observable(data.IsSubDomain);

}

Later on I'm trying to extend SiteModel but it bombs out saying
"IsSubDomain is not a function". Is this even possible? I know I could
put the fullDomainName inside the SiteModel closure but I'm trying
some automation so I need to be able to extend the model's prototype
after its defined.

        SiteModel.prototype.fullDomainName = ko.computed(function () {
            return (self.toString()); // returns the window object...
            if (this.IsSubDomain() && this.DomainName()) { // bombs
out here
                return this.DomainName() + ".myCompanyWebsite.com";
            }
            else {
                return this.DomainName();
           }
        }, this);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
StrandedPirate  
View profile  
 More options Apr 23 2012, 6:24 pm
From: StrandedPirate <joey_brads...@strandedpirate.com>
Date: Mon, 23 Apr 2012 15:24:30 -0700 (PDT)
Local: Mon, Apr 23 2012 6:24 pm
Subject: Re: How do I add a computed property via the prototype?
Here is a fiddle showing the issue: http://jsfiddle.net/StrandedPirate/J44S4/2/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lauer  
View profile  
 More options Apr 25 2012, 9:14 am
From: Bob Lauer <rla...@gmail.com>
Date: Wed, 25 Apr 2012 06:14:27 -0700 (PDT)
Local: Wed, Apr 25 2012 9:14 am
Subject: Re: How do I add a computed property via the prototype?

I tried to get this working using prototype, but I can't, and I'm not sure
it's even possible.  

The advantage of adding a function to the SiteModel's prototype property is
that it will be shared across all instances of the SiteModel.  However, if
you're only going to have one instance of the SiteModel, then that
advantage doesn't really come into play.  If that's the case, you can do
something like this:

var model = new SiteModel("someUrl", someDataObject);
model.fullDomainName = ko.computed(function() {
   // Do something in here

}, model);

Just make sure you pass in model as the 2nd parameter to ko.computed, which
will set the "this" variable to your model object inside of your function
that you passed to ko.computed.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rpn  
View profile  
 More options Apr 25 2012, 9:54 am
From: rpn <rnieme...@gmail.com>
Date: Wed, 25 Apr 2012 06:54:42 -0700 (PDT)
Local: Wed, Apr 25 2012 9:54 am
Subject: Re: How do I add a computed property via the prototype?

observables and computed observables are functions that store some state
internally, which means that they can't really be put on the prototype like
a normal function or property could.

I have seen some people create functions on the prototype that add
observables to the instance and then call some kind of "init" when the
object is created to call these functions.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Best  
View profile  
 More options Apr 25 2012, 8:54 pm
From: Michael Best <mb...@dasya.com>
Date: Wed, 25 Apr 2012 17:54:15 -0700 (PDT)
Local: Wed, Apr 25 2012 8:54 pm
Subject: Re: How do I add a computed property via the prototype?
Here's one way to do it: http://jsfiddle.net/mbest/J44S4/29/

I've defined the function in the prototype and made it a computed
observable in the constructor.

Here's a way to do it a more generic way: http://jsfiddle.net/mbest/XEez7/

-- Michael

On Apr 23, 12:24 pm, StrandedPirate <joey_brads...@strandedpirate.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lauer  
View profile  
 More options Apr 25 2012, 9:07 pm
From: Bob Lauer <rla...@gmail.com>
Date: Wed, 25 Apr 2012 18:07:11 -0700 (PDT)
Local: Wed, Apr 25 2012 9:07 pm
Subject: Re: How do I add a computed property via the prototype?

Both are clever solutions.

It's worth noting that the above methods have three drawbacks though:

1. It extends Object.prototype.  This is widely considered bad practice.  Here's
a blog post about why not to do that.<http://blog.dreasgrech.com/2009/11/no-one-should-extend-objectprototy...>  
I will admit that this can be worked around very easily, but thought it was
still worth mentioning.
2. The nice part about functions that are added to the prototype object is
that only one function exists across all instances, instead of each
instance having its own copy of the function.  In this case, however,
a copy of the observable function will be created for each instance of the
SiteModel that is instantiated.
3. The other nice part about functions that are added to the prototype
object is all instances of the object will be able to use them, even if
those instances were instantiated before the method was added to the
prototype object.  However, all methods added to the SiteModel.prototype
will not be converted to computed functions on all already-existing
instances of SiteModel, since that conversion happens in the constructor.
 Depending on when you define the function on the prototype object and when
you instantiate your object, you may or may not have that computed function
available on your object.

Truthfully, I don't see any advantage to defining functions on the
prototype.  You end up creating copies of the function anyway, and you just
make your code more confusing.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Best  
View profile  
 More options Apr 25 2012, 9:31 pm
From: Michael Best <mb...@dasya.com>
Date: Wed, 25 Apr 2012 18:31:04 -0700 (PDT)
Local: Wed, Apr 25 2012 9:31 pm
Subject: Re: How do I add a computed property via the prototype?
Bob, good points. Computed observables should be treated like
properties of an object instance rather than methods of the object.
But since they are defined via a function, the prototype is a
convenient place to put those functions. In response to your points:

1. I don't extend Object in my own code, but it was a convenient way
to demonstrate it. Thanks for pointing that out.

2. The underlying read function of the computed *will* be shared
although the actual computed property won't. I suppose there could be
confusion if you created some objects and then changed the function in
the prototype. New objects would use the new function, but old objects
wouldn't.

3. Since computed observables are properties, you shouldn't expect to
be able to add them to existing objects through the prototype.

Thanks,
Michael


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »