-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow route to be defined but not require a callback method on the router #659
Conversation
…lback method for the route name
If you're using jQuery you can also do the following, which IMO is better because you are explicitly saying "I do not need a real callback". Or even better, while you have Underscore for sure, use events: {
"/idonothaveacallback" : $.noop
} |
@yuchi - i must be misunderstanding what you're suggesting... there's no support for routes: {
"/foo/bar": "foobar"
} this raises a |
Oh, sorry. I misunderstood your needs. You are actually right. |
+1 |
1 similar comment
+1 |
Is there any advantage to defining the routes only to attach a handler via bind? I guess I don't see the difference between what you're describing and defining a route from another class with |
it's all about coupling. I'm building a composite application right now. There's a treeview on the left, a grid on the top and a form on the bottom. Each of these areas has their own distinct set of functionality, and are separated into their own JavaScript modules. When building the routes to manage this app, I need each of my areas to respond correctly to the route being fired. I can either do this by coupling all three of my areas directly to the router and have the router callback method call all three of the modules to do something, or I can decouple everything through events. When the router fires an event for the route, each of the modules can listen to that event and separately respond in order to put themselves into the correct state and display. Right now, I have to put empty callback methods in my router to make this work. It's ugly and extra code that does nothing. I'm hoping that, since the change I made is only 1 if-statement around the callback being called, that this will make it in to the next release. |
What about associating the routing relationships through a generic (tho non-evented) interface, such as a generic match in your router code? That's how I've handled similar situations in the past:
And then attach the instance of any singleton View directly to your application namespace, named after the route that matches it. The point being that you can use the router as-is to establish routes to decoupled modules. Happy to discuss different patterns for achieving this... cc/ @tbranyen |
sure, there are dozens of ways to decouple things. i'm saying the change i made is the only way. i'm only saying that this one little if statement is such a small change for such a large benefit, that it should be pulled in. |
This may seem simliar to @yuchi's comment, but can't your event binding approach be achieve by binding the dummy routes?
IMO this patch seems to serve a very specific edge case... |
What does "edge case" have to do with anything, though? This is a non-breaking change that simplifies things for a particular scenario. The |
@derickbailey is there a use case where assigning the route to a NoOp like in my example above is insufficient? |
I'm afraid the proposed change here would lead to really nasty bugs for a number of folks. The contract with binding events, like the OP's:
... is that the
|
It's not about "insufficient". All of the workarounds that I am currently using, and the ones you have provides, work. There's no questioning that. Here's a much more detailed scenario, via code, illustrating why I want this. This is a simplification of an app that I wrote, showing an evented architecture (note that I'm writing this directly into the issues list so it probably won't run as is, but it should get the point across).
When a user clicks on an image in an This code works... my app works this way. But, in this version of the code, my router is directly coupled to an instance of MainView. This is less desirable than what I want. I would prefer to write this version of the router / app objects instead:
It's a few lines of code cleaner in this version, using events instead of coupling the objects together directly. As the application grows and additional areas are added to the screen, the benefit becomes larger. |
Alright -- thanks for the persistence and persuasion. |
(deleted most recent comment... i sent it in after Jeremy had re-opened this. thanks, jeremy!) |
I'm using a router without using callback methods in an example app that I'm building, and I don't want to be required to build a callback method on my router.
for example, i want this to work:
without defining a
bar
andbazIt
function on the router, this would cause errors because the callback is undefined.this patch adds support for using a router like this, which then allows me to bind to the router events independently of the router configuration:
this patch includes the test and implementation to cover this.