Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : can.compute declared as observe method loses right context when called in view

$
0
0
This is expected behavior because "this" doesn't mean anything in the first example.  

But, you probably don't need to create "votes" as a compute.  A normal function will be fine:


  1. votes : function() {
  2. return this.attr('upvotes') - this.attr('downvotes');
  3. };
Doing something like:


<%= commander.votes() %>


will already be live-bound.  You don't need to declare functions as computes unless you plan to bind on them yourself.


If you do need to turn an instance's method into a compute, do:

var voteCompute = can.compute(commander.votes,commander);

voteCompute.bind("change",function(){})




BTW, it's not documented, but you can change any attribute into a compute like:

commander.compute("upvotes")


It could be useful to also allow:

commander.compute("votes")


Viewing all articles
Browse latest Browse all 3491

Trending Articles