canjs can.route and can.route.delegate, listen when property has a particular value?

188 Views Asked by At

I use canJs with plugin can.Map.delegate and i would like this, but it doesn't work, "ok" is not displaying in console.

Could you help me ?

    $(function() {

      var Routing = can.Control({
        '{can.route} id=3': function(data) {
          console.log("ok");
         }
      });

      var routeDelegate = can.route(":id");
      can.route.ready();
      routeDelegate.delegate("id", "set");

      new Routing(document);

      can.route.attr("id", 3);

    });

Thank

1

There are 1 best solutions below

0
On

What about using can.Control.route?

http://canjs.com/docs/can.Control.route.html

$(function() {

    var Routing = can.Control({
        ':id route': function(data) {
            console.log(data); // logs: Object { id="3"}
        }
    });

    new Routing(document);

    can.route.ready();

    can.route.attr("id", 3);

});

This does all the simple mapping for you, you then have the params in the data object.

There is a nice 2 pager on this with both types of routes here:

http://bitovi.com/blog/2012/05/hashchange-routing-can-route-1.html


Note: I moved can.route.ready(); to a bit later so a browser refresh will trigger the Control