Skip to content
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

Accessing View Class Methods via poirot JS #14

Closed
TylerLH opened this issue Dec 6, 2011 · 3 comments
Closed

Accessing View Class Methods via poirot JS #14

TylerLH opened this issue Dec 6, 2011 · 3 comments

Comments

@TylerLH
Copy link

TylerLH commented Dec 6, 2011

Hopefully my last issue. Any help would be greatly appreciated.

I'm trying to access my helper methods in my optional view class. For example:

# View Class
module Users
  class UserListView < Poirot::View
    def foo
      "bar"
    end
  end
end
// JS call (coffeescript)
users = poirot.userList()

When loading the template with data, anything I define inside the JS obviously makes it into the view except for {{ foo }}.

@olivernn
Copy link
Owner

olivernn commented Dec 7, 2011

When you define helper methods in your view class they are available in ruby, but they aren't automatically available in JavaScript. I'm not sure there is anyway to do this automatically.

In your javascript you could create your own view presenter object to pass to the template, something like this:

var userListView = {
  foo: 'bar'
}

var users = poirot.userList(userListView)

This presenter object could be more dynamic if you need to pass a list of users to your user list view:

var userListViewPresenter = function (user) {
  return {
    fullName: [user.firstName, user.lastName].join(' ')
  }
}

var users = poirot.userList({
  users: users.map(userListViewPresenter)
})

Finally you could also define an as_json method on your ruby view class, and then expose this to your JavaScript layer.

@olivernn
Copy link
Owner

I'm closing this for now, if your still having problems feel free to re-open.

@TylerLH
Copy link
Author

TylerLH commented Dec 14, 2011

Thanks for the notes:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants