Problem with data-bind in a show page

14 views
Skip to first unread message

pmv...@gmail.com

unread,
Oct 8, 2014, 11:44:13 PM10/8/14
to batm...@googlegroups.com
Hi all, first of all I am a newbie trying to get a simple batmanjs + rails 4 app and running but I came across several issues that despite reading the documentation and few several blogs couldn't solve.

The situation is like this, I have a rails REST controller that handles location resources (location, description, latitude, longitude), that's actually working fine and serves correct JSON responses.

Then I setup a batman app inside my rails application, scaffolded a Location resource, with controller, views, and model, but in some places of my pages the location data is not being displayed with data-bind.

Here is what i have so far:

app/assets/batman/models/location.js.coffee
class Sightseer.Location extends Batman.Model
@resourceName: 'locations'
@storageKey: 'locations'

@persist Batman.RailsStorage

@validate "description", { presence: true }
@validate "location", { presence: true }

# Use @encode to tell batman.js which properties Rails will send back with its JSON.
# @encode 'name'
@encode "description", "latitude", "location", "longitude"
@encodeTimestamps()

app/assets/batman/controllers/locations_controller.js.coffee
class Sightseer.LocationsController extends Sightseer.ApplicationController
routingKey: 'locations'

@beforeAction 'findLocation', only: ['show', 'edit']

index: (params) ->
@set('locations', Sightseer.Location.get('all'))

show: (params) ->

edit: (params) ->

new: (params) ->
@set('location', new Sightseer.Location)

create: (params) ->
@location.save (errors, record) =>
if errors
throw errors unless errors instanceof Batman.ErrorsSet
else
@redirect record

update: (params) ->
@location.save (errors, record) =>
if errors
throw errors unless errors instanceof Batman.ErrorsSet
else
@redirect record

destroy: (node, event, context) ->
if context.get('location')
location = context.get('location')
else
location = @location
location.destroy (errors) =>
if errors
throw errors unless errors instanceof Batman.ErrorsSet
else
@redirect '/locations'

findLocation: (params) ->
Sightseer.Location.find params.id, (error, record) =>
@set 'location', record

apps/assets/batman/html/locations/show.html (The page with the issue)
<h1 data-bind="location.id"></h1>
<h1 data-bind="location.location"></h1>

<p data-bind="location.description"></p>>

<a data-route="routes.locations[location].edit">Edit</a> |
<a data-event-click="destroy">Destroy</a>

Whenever I hit /locations/2

I get from Rails the followin json:

{"id":2,"author_id":null,"description":"test description","latitude":40.7143528,"location":"test location 2","longitude":-74.0059731,"created_at":"2014-10-09T02:26:31.352Z","updated_at":"2014-10-09T02:26:31.352Z"}

I can see in the show.html page that the "id" attribute is bound, but the "location" and "description" are not, the attributes are showing fine in index.html page.

Is there something I am missing? Is anything wrong in the controller and / or the view?

Thanks
Pablo

Pablo Viva

unread,
Oct 9, 2014, 9:55:12 AM10/9/14
to batm...@googlegroups.com
I've tested the very same controller and show.html but instead of using Batman.RailsStorage I used Batman.LocalStorage and created a few records on my own on the "run" event of the App and everything works fine.

So my guess There's something wrong with the JSON returned by the rails endpoint, how can I validate the JSON returned is a valid JSON expected by RailsStorage?

Thanks
Pablo

Pablo Viva

unread,
Oct 9, 2014, 10:03:04 AM10/9/14
to batm...@googlegroups.com
Actually I made it work, the returned JSON for the endpoint /locations/2.json was missing the root element:

I was returning:

{"id":2,"author_id":null,"
description":"test description","latitude":40.7143528,"location":"test location 2","longitude":-74.0059731,"created_at":"2014-10-09T02:26:31.352Z","updated_at":"2014-10-09T02:26:31.352Z"}

But apparently RailsStorage expected:

{"location":{"id":2,"author_id":null,"description":"test description","latitude":40.7143528,"location":"test location 2","longitude":-74.0059731,"created_at":"2014-10-09T02:26:31.352Z","updated_at":"2014-10-09T02:26:31.352Z"}}

Thanks
Pablo
Reply all
Reply to author
Forward
0 new messages