Shopify/batman — Issue #298: Problem with resources
Author: madtrick
Created: 2012-01-02 16:02:31+00:00
State: open

Hi,

I'm starting with Batmanjs and after some coding I got stuck with the resources. The involved code is this:

App,

```coffee
class TwitterBatmanApp extends Batman.App
@set 'mission', 'fight crime'

@global yes

@controller 'users'
@model 'user'

@resources 'users'
```

User model,

```coffee
class TwitterBatmanApp.User extends Batman.Model

@encode 'name'
@persist Batman.LocalStorage
```

Users controller,

```coffee
class TwitterBatmanApp.UsersController extends Batman.Controller

@NULL_USER = "---"
index: (params) ->

usersChange: (selector) ->
if (userid = $(':selected', selector).val()) isnt UsersController.NULL_USER
@redirect TwitterBatmanApp.User.find userid, (user) ->
user
```

The problem is that when running the redirect method (inside usersChange) an error is thrown,

```javascript
Uncaught TypeError: Cannot call method 'indexOf' of undefined
```
This error is thrown inside Dispatcher#findRoute as the url param is undefined.

One thing I noticed is that inside Dispatcher#findUrl method (which finds the url given to Dispatcher#findRoute) the params argument (which on my case is a User instance) should have a resource property (since I set it up as a resource) but it hasn't . So none url is matched.

```coffee
findUrl: (params) ->
for url, route of @routeMap
matches = no
options = route.options
if params.resource #issuer note: params (an User instance) must have a resource property, mustn't it?
matches = options.resource is params.resource and
options.action is params.action
else
...
```

Any idea about what's going on?

P.S.: In the documentation, when you introduce the resources method, your give an example of its integration with the redirect method by finding a Gadget. In that code, you don't provide a callback to the find method which is required. It would be nice if you fix that.