1

Am looking at the Canjs Sample for Control.

TaskStriker = can.Control({
    "{task} completed": function(){
        this.update();
    },
    update: function(){
        if ( this.options.task.completed ) {
            this.element.addClass( 'strike' );
        } else {
            this.element.removeClass( 'strike' );
        }
    }
});
var taskstriker = new TaskStriker({ 
    task: new Task({ completed: 'true' }) 
});

In this case what is the Task object exactly?. I tried creating the Task with the can.Construct but it is not triggering the update function when value is changed.

Can some one please explain a bit on this?.

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
Aravind R
  • 716
  • 1
  • 10
  • 36

2 Answers2

3

can.Construct does not implement observable properties.

The task object will be a can.Map (http://canjs.com/docs/can.Map.html) or a can.Model (http://canjs.com/docs/can.Model.html). With those two you are able to observe changes on the object and thus trigger the update code.

Sebastian
  • 2,249
  • 17
  • 20
1

A couple things stand out: (1) Your task object is having completed set before taskstriker is initialized; and (2) to instantiate a can.Control or one of its subclasses, you need to pass in a DOM element as the first argument, and the options object as the second.

air_hadoken
  • 611
  • 4
  • 5
  • This is the example given in the canjs page.Please reffer the link below http://canjs.com/docs/can.Control.prototype.on.html – Aravind R Jan 22 '14 at 19:30
  • 1
    The docs are incorrect on this. Go report it at https://github.com/bitovi/canjs.com/issues -- meanwhile here's an example that works: http://jsfiddle.net/air_hadoken/mXzD7/1/ – air_hadoken Jan 22 '14 at 21:22
  • one more small question.So what happens when we do this step can.Observe.extend("Task"). You haven't defined this object Task rt?. So what happens here ?.Can you please explain that as well. – Aravind R Jan 24 '14 at 17:26
  • 1
    That line is defining the Task class as a subclass of can.Observe. It creates .Task as a constructor function with no additional static or prototype properties over what can.Observe already has. – air_hadoken Jan 24 '14 at 19:11