delegate can/observe/delegate

 

The delegate plugin allows you to listen to more specific event changes on Observes. It allows you to specify:

  • the attribute or attributes - that you want to listen to and optionally the value you want it to match
  • the type of event (add,set,remove,change)

Listen to specific event changes with delegate(selector, event, handler(ev,newVal,oldVal,from)) :

// create an observable
var observe = new can.Observe({
  name : {
    first : "Justin Meyer"
  }
})
  var handler;
//listen to changes on a property
observe.delegate("name.first","set", 
  handler = function(ev, newVal, oldVal, prop){

  this   //-> "Justin"
  ev.currentTarget //-> observe
  newVal //-> "Justin Meyer"
  oldVal //-> "Justin"
  prop   //-> "name.first"
});

// change the property
observe.attr('name.first',"Justin")

Delegate will listen on the object until you call undelegate(selector, event, handler) to remove the event handler.

observe.undelegate("name.first","set", handler );