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

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

$
0
0
  1. Commander = can.Model({
  2. attributes : {
  3. upvotes : 'number',
  4. downvotes : 'number',
  5. like : 'boolean'
  6. }
  7. }, {
  8. votes : can.compute(function() {
  9. return this.attr('upvotes') - this.attr('downvotes');
  10. })
  11. });
I call votes() in controllers code everyting is ok

But if call votes() in EJS it will say that object has no attr
If I refactor code like that, it will work:

  1. Commander = can.Model({
  2. attributes : {
  3. upvotes : 'number',
  4. downvotes : 'number',
  5. like : 'boolean'
  6. }
  7. }, {
  8. init : function() {
  9. var self = this;
  10. this.votes = can.compute(function() {
  11. return self.attr('upvotes') - self.attr('downvotes');
  12. })
  13. }
  14. });
Is it a bug or this can not be avoided?

Viewing all articles
Browse latest Browse all 3491

Trending Articles