Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining two separate routers with can.Control.route in CanJS

I have two routes defined in different JS-documents, although both are included into the same HTML-file. Route 1 looks like this:

Router = can.Control({
   "invoices/:id route": function(data){
    //load an invoice by id
   }
 });

And the other one like this:

Router = can.Control({
   "receipts/:id route": function(data){
    //load a receipt by id
   }
 });

When i browse to #!receipts/1 both Receipts and Invoices are being instantiated. Why is that and how can I fix it?

like image 323
Anton Gildebrand Avatar asked Dec 06 '25 10:12

Anton Gildebrand


1 Answers

You won't find this in the CanJS docs because it's a basic javascript issue.

Please note that even though the variables are declared in different scripts, the fact that they are included in the same page means that they both live in the same global namespace, thus the second Control object declaration overwrites the first.

The simplest answer is to assign each Control object to a different variable (e.g., Router1, Router2).

You'd also be better off combining both declarations in one file to improve performance.

like image 184
supermasher Avatar answered Dec 08 '25 01:12

supermasher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!