olivernn / poirot

mustaches in your rails
http://olivernn.github.com/poirot
108 stars 21 forks source link

Accessing View Class Methods via poirot JS #14

Closed TylerLH closed 12 years ago

TylerLH commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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

TylerLH commented 12 years ago

Thanks for the notes:)