Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding in controller before filter params for all views #174

Closed
srad opened this Issue · 6 comments

4 participants

@srad

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

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

@mde
Owner

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
Owner

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 "this.foo" 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

Looks really great, thank you.

@Techwraith Techwraith closed this
@iDVB

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

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

    this.before(function(){
      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
Owner

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;
    next();
  }, {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.