Understanding Canjs Control sample

485 views Asked by At

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?.

2

There are 2 answers

0
Sebastian On BEST ANSWER

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.

4
air_hadoken On

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.