Accessing variables in controller with @get

11 views
Skip to first unread message

Pablo Viva

unread,
Oct 9, 2014, 1:22:43 PM10/9/14
to batm...@googlegroups.com
Hi, I've read a few of the Batman.Controller documentation pages and I didn't find any good example of getting variables with @get inside a Controller.

The use case I am having is this, in a rails app in any nested controller you have, lets say PhotosController:

class PhotosController < ApplicationController
  before_action :set_post, only: [:index, :new, :create, :edit:, :update]

  def index
     @photos = @post.photos
  end
  ...
  ...
  private
    def set_post
        if params[:post_id].present?
          @post = Post.find(params[:post_id])
        end
    end
end

I want to replicate that using Batman.js so I have a PhotosController:

class Sightseer.PhotosController extends Sightseer.ApplicationController
  routingKey: 'photos'

  @beforeAction 'findPost', only: ['index', 'new', 'create']

  index: (params) ->
    post = @get('post')
    alert(post)
    @set('photos', post.get('photos'))
  ...
  ...
  ...
  findPost: (params) ->
    Sightseer.Post.find params.postId, (error, record) =>
      @set 'post', record
      alert(record)

When I execute this code and I access posts/1/photos I can see the alert in the findPost method executes correctly and displayes the post.id, but the alert inside the index action displays undefined, like the @get('post') returned an undefined object.

Why is that?

How do I access in the controller a variable I've set with @set 'name', value?

Thanks
Pablo
Reply all
Reply to author
Forward
0 new messages