Confused about how routes are supposed to be used

351 views
Skip to first unread message

Matt Zukowski

unread,
Mar 28, 2012, 11:00:34 PM3/28/12
to batm...@googlegroups.com
I'm super confused about how I'm supposed to be using routes in my views.

For example, I have this in my app.coffee:

class  MyApp extends Batman.App
 
@global yes

 
@controller 'foos'
 
@model 'foo'
 
@resources 'foos'


Then under views/foos/index.html, I want to have a link that points to the "new" form. I've seen all of the following in examples:

<a data-route="foos#new">Add Foo</a>
<a data-route="routes.foos.new">Add Foo</a>
<a data-route="/foos/new">Add Foo</a>


But none of these work. I just get <a href="#">Add Foo</a>. The only one I've managed to actually get working is:

<a data-route="{'controller':'foos','action':'new'}">Add Foo</a>

Seems kind of awkward, but whatever.

Now I'm trying to add a link to the "edit" form for each Foo. And I can't get this to work at all. I've tried:

<div data-foreach-foo="Foo.all">
<a data-route="{'controller':'foos','action':'edit','id':foo.id}">Foo</a>
<a data-route="{'controller':'foos','action':'edit','id':id}" data-bind-id='foo.id'>Foo</a>
<a data-route="routes.foos[foo].edit">Foo</a>
</div>


... but no dice. Something feels really wrong here. Am I doing something wrong?

I'm using the latest batman.js installed from GitHub.

Matt Zukowski

unread,
Mar 28, 2012, 11:08:52 PM3/28/12
to batm...@googlegroups.com
P.S. looks like this gets me part of the way there: https://github.com/Shopify/batman/commit/5fa946245b59165ccc3e0aece58b7751fb8f4985

but would be great to get some clarification on the different ways routes are meant to be used inside views now after this change

Michael van Rooijen

unread,
Apr 6, 2012, 8:24:33 PM4/6/12
to batman.js
Hi Matt,

Have you ever gotten around to solving this issue? I'm running in to
it myself and am wondering what a good way of solving this might be.

Thanks!


On Mar 29, 5:08 am, Matt Zukowski <matt.zukow...@gmail.com> wrote:
> P.S. looks like this gets me part of the way
> there:https://github.com/Shopify/batman/commit/5fa946245b59165ccc3e0aece58b...

Michael van Rooijen

unread,
Apr 6, 2012, 9:00:31 PM4/6/12
to batman.js
Actually this works for me (in 0.9)

<ul>
<li data-foreach-item="items"><a data-bind="item.task" data-
route="task" /></li>
</ul>

This will generate the proper urls to the give item resources. But
this is freaking ugly:

<a data-route="{'controller' : 'items', 'action': 'index'}">All items</
a>

Going from the comments from the commit you linked, it looks like you
could do this (and it works) to get New and Edit links going:

<a data-route="Item | routeToAction 'new'" />
<a data-route="item | routeToAction 'edit'" />
<a data-route="item | routeToAction 'destroy'" /> <--- Then this does
NOT work. So annoying. How do you point at the destroy action? :/

So many parts of the Batman.js framework at this time are just so time
consuming because I'm _trying_ (and failing) to figure out why things
don't work and how it should be done.
Having to resort to that JSON syntax in data-route feels like a hack,
pretty sure it's not meant to be done that way but guess there's no
other choice at this time since it's freaking hard to figure out how
to do it any other way.

Also when I run: MyApp.get("routes") <-- this returns "undefined". So
MyApp.get("routes.items") also returns "undefined" while the docs
indicate it actually does return something. It's just tiresome. I
don't know whether the docs are in-accurate, or the API is just being
changed every week and docs aren't being updated, or whether it's a
bug in the framework.

If you got any info on how to go about routing since your last post,
then that'd be nice!

Thanks!

On Mar 29, 5:08 am, Matt Zukowski <matt.zukow...@gmail.com> wrote:
> P.S. looks like this gets me part of the way
> there:https://github.com/Shopify/batman/commit/5fa946245b59165ccc3e0aece58b...

luxerama

unread,
Apr 14, 2012, 3:27:49 AM4/14/12
to batm...@googlegroups.com
I would also like some clarification on this matter. If possible from someone working on the project at the moment so we can get an idea of the direction this is taking as there seems to have been a lot of change on routing recently.

luxerama

unread,
Apr 20, 2012, 7:19:27 AM4/20/12
to batm...@googlegroups.com
For anyone interested, I have created a helper that should be able to deal with most routing applications that are not covered by the tools provided by batman.js itself.


Please let me know if you have any questions/improvements or found problems with it.


On Thursday, 29 March 2012 04:00:34 UTC+1, Matt Zukowski wrote:

Anderson Florence

unread,
Apr 20, 2012, 9:24:20 AM4/20/12
to batm...@googlegroups.com
It sounds like you're fighting the framework. Resources imply a RESTful design where the http verbs imply the appropriate CRUD action. Instead of using a 'new' action, you should do a POST to '/foos'

If you truly want it to route to a custom 'new' controller action I would suggest just using a @route statement in the app. However, for CRUD operations, you would be breaking RESTful design best practices if you care about that sort of thing.

Here is a link explaining how Rails Routes and Resources work: http://guides.rubyonrails.org/routing.html

Sent from my iPhone

luxerama

unread,
Apr 20, 2012, 10:46:23 AM4/20/12
to batm...@googlegroups.com
This is not meant for API endpoins, so there is in REST nor CRUD involved. I get your point if I was to, lets say create a CMS. The routeTo filter is to be used when creating a batman.js application in order to achieve things such as pretty URLs.
For example, I have a post which has a collection of attachments. Now each attachment should have its on URL within the post like so /posts/post-identifier/attachements/attachement-identifier. This would require me to have a URL with multiple arguments like the ones described in the examples.

I'm not sure if I have understood your argument completely, however I cannot see this requirement to be anything out of the ordinary nor can I see it breaking any conventions. The REST endpoints will all stay the same, thus rails will be happy.


On Friday, 20 April 2012 14:24:20 UTC+1, anderson_f wrote:
It sounds like you're fighting the framework. Resources imply a RESTful design where the http verbs imply the appropriate CRUD action. Instead of using a 'new' action, you should do a POST to '/foos'

If you truly want it to route to a custom 'new' controller action I would suggest just using a @route statement in the app. However, for CRUD operations, you would be breaking RESTful design best practices if you care about that sort of thing.

Here is a link explaining how Rails Routes and Resources work: http://guides.rubyonrails.org/routing.html

Sent from my iPhone

Anderson Florence

unread,
Apr 25, 2012, 2:15:32 PM4/25/12
to batm...@googlegroups.com
So in my first look at data-route attribute, the github intro docs said it expected either a model instance or a model class. Because it didn't seem like it was designed to accept a controller action I was thinking it was determining its action based on the http verb.

Later, I saw where you were talking about where some examples(classifieds) have used it as though it was a controller and action. Though the link for the demo of that example doesn't seem to work.

Bottom line; I agree that it would be nice if we could get some clarification on how the data-route attribute is intended to be used.

I did notice some recent commits regarding a new syntax for accessing named routes. I'm not sure if it is actually related or not

Sent from my iPhone

Xavier Spriet

unread,
Apr 25, 2012, 2:32:43 PM4/25/12
to batm...@googlegroups.com
I think at the moment, a good approach is to send data-route a
NamedRouteQuery,
which you can infer from an arbitrary controller action through
App.get('routes.controllerName.actionName').

You can also use App.get('currentRoute.controller') and
App.get('currentRoute.action') to get the current controller and action
strings.



On Wed Apr 25 14:15:32 2012, Anderson Florence wrote:
> So in my first look at data-route attribute, the github intro docs
> said it expected either a model instance or a model class. Because it
> didn't seem like it was designed to accept a controller action I was
> thinking it was determining its action based on the http verb.
>
> Later, I saw where you were talking about where some
> examples(classifieds) have used it as though it was a controller and
> action. Though the link for the demo of that example doesn't seem to work.
>
> Bottom line; I agree that it would be nice if we could get some
> clarification on how the data-route attribute is intended to be used.
>
> I did notice some recent commits regarding a new syntax for accessing
> named routes. I'm not sure if it is actually related or not
>
> Sent from my iPhone
>
> On Apr 20, 2012, at 10:46 AM, luxerama <vin...@siebert.im
> <mailto:vin...@siebert.im>> wrote:
>
>> This is not meant for API endpoins, so there is in REST nor CRUD
>> involved. I get your point if I was to, lets say create a CMS. The
>> routeTo filter is to be used when creating a batman.js application in
>> order to achieve things such as pretty URLs.
>> For example, I have a post which has a collection of attachments. Now
>> each attachment should have its on URL within the post like so
>> //posts/post-identifier/attachements/attachement-identifier/. This
>> would require me to have a URL with multiple arguments like the ones
>> described in the examples.
>>
>> I'm not sure if I have understood your argument completely, however I
>> cannot see this requirement to be anything out of the ordinary nor
>> can I see it breaking any conventions. The REST endpoints will all
>> stay the same, thus rails will be happy.
>>
>> On Friday, 20 April 2012 14:24:20 UTC+1, anderson_f wrote:
>>
>> It sounds like you're fighting the framework. Resources imply a
>> RESTful design where the http verbs imply the appropriate CRUD
>> action. Instead of using a 'new' action, you should do a POST to
>> '/foos'
>>
>> If you truly want it to route to a custom 'new' controller action
>> I would suggest just using a @route statement in the app.
>> However, for CRUD operations, you would be breaking RESTful
>> design best practices if you care about that sort of thing.
>>
>> Here is a link explaining how Rails Routes and Resources work:
>> http://guides.rubyonrails.org/routing.html
>> <http://guides.rubyonrails.org/routing.html>
>>
>> Sent from my iPhone
>>
>> On Apr 20, 2012, at 7:19 AM, luxerama wrote:
>>
>>> For anyone interested, I have created a helper that should be
>>> able to deal with most routing applications that are not covered
>>> by the tools provided by batman.js itself.
>>>
>>> https://gist.github.com/2422262 <https://gist.github.com/2422262>
>>>
>>> Please let me know if you have any questions/improvements or
>>> found problems with it.
>>>
>>> On Thursday, 29 March 2012 04:00:34 UTC+1, Matt Zukowski wrote:
>>>
>>> I'm super confused about how I'm supposed to be using routes
>>> in my views.
>>>
>>> For example, I have this in my app.coffee:
>>>
>>> |
>>> classMyAppextendsBatman.App
>>> @globalyes
>>>
>>> @controller'foos'
>>> @model'foo'
>>> @resources'foos'
>>> |
>>>
>>>
>>> Then under views/foos/index.html, I want to have a link that
>>> points to the "new" form. I've seen all of the following in
>>> examples:
>>>
>>> |
>>> <adata-route="foos#new">Add Foo</a>
>>> <adata-route="routes.foos.new">Add Foo</a>
>>> <adata-route="/foos/new">Add Foo</a>
>>> |
>>>
>>>
>>> But none of these work. I just get <a href="#">Add Foo</a>.
>>> The only one I've managed to actually get working is:
>>>
>>> |
>>> <adata-route="{'controller':'foos','action':'new'}">Add Foo</a>
>>> |
>>>
>>> Seems kind of awkward, but whatever.
>>>
>>> Now I'm trying to add a link to the "edit" form for each
>>> Foo. And I can't get this to work at all. I've tried:
>>>
>>> |
>>> <divdata-foreach-foo="Foo.all">
>>> <adata-route="{'controller':'foos','action':'edit','id':foo.id
>>> <http://foo.id>}">Foo</a>
>>> <adata-route="{'controller':'foos','action':'edit','id':id}"data-bind-id='foo.id
>>> <http://foo.id>'>Foo</a>
>>> <adata-route="routes.foos[foo].edit">Foo</a>
Reply all
Reply to author
Forward
0 new messages