Skip to content

Adding in controller before filter params for all views #174

srad opened this Issue Aug 20, 2012 · 6 comments

4 participants

srad commented Aug 20, 2012

I'm tying to toggle the visibility of certain elements within the main template view based on if the user is logged in or not.

The question is if I can add some kind of parameters which are passed to all views within the before filter.

Otherwise I had to add to each controller action "params.loggedIn = true".

Is there a nicer way to accomplish this?

srad commented Aug 20, 2012

I inverted the whole logic, so that is a way to take the smaller problem.

mde commented Aug 20, 2012

Ah, I was just tripping over this same sort of problem yesterday. I wanted something that appeared persistently in the layout, without having to pass it every time. Mine only involves three endpoints, so it ended up not being a big deal to pass it in every case, but I can imagine this being a pain in the ass for other people.

One solution if the data is global to the app is to stick it on the global 'geddy' object, but that doesn't work for session- or request- specific data. I think your workaround is a good one, but I would also like a better solution for this.

mde commented Aug 21, 2012

Have a look at the work going on in the "remove-model" branch. I've added some code so that all instance-properties on the controller (locally set properties, not ones inherited from BaseController) are copied onto the data object for rendering the views. That means if you set "" in your controller logic, you'll have that as a local "foo" variable in your view.

So you can set something like a "this.authenticated" property in a before filter in your Application controller, and it will be available in all your views. (You still have to pass it down to partials.)

I'm not sure when this branch will land, but it should be pretty soon.

srad commented Aug 22, 2012

Looks really great, thank you.

@Techwraith Techwraith closed this Sep 5, 2012
iDVB commented Nov 29, 2013

Is this part of the 0.11.3 build?
In my controller I...

this.index = function (req, resp, params) {
    var self = this;

      self.isAuthed = 'test';
    }, {});

and in my template I...

  <%= this.isAuthed %>

and it does not output anything?

I tried this in the template too...

  <%= isAuthed %>

and I get an undefined error.

mde commented Nov 29, 2013

The before method probably shouldn't be invoked inside your action. Calling before adds a 'before' filter that gets invoked before any actions are called. If you want that property to get set on all controllers as a part of the normal request/response cycle, put that call to before inside the Application controller:

var Application = function () {
  this.before(function (next) {
    this.isAuthed = true;
  }, {async: true});

The 'before' filter is invoked on your controller instance, so "this" references it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.