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

Global context with request argument #59

Closed
g6123 opened this issue Sep 28, 2015 · 2 comments
Closed

Global context with request argument #59

g6123 opened this issue Sep 28, 2015 · 2 comments
Assignees
Labels
Milestone

Comments

@g6123
Copy link

@g6123 g6123 commented Sep 28, 2015

When global context was first suggested in #4, functions that generate context could have a request argument. Like this :

server.views({
  context: function (request) {
    return {
      version: '1.4.5',
      date: Date.now(),
      id: request.id
    }
  }
});

But it is not actually implemented now, and I think that idea is quite essential in many cases of developments.

@jagoda

This comment has been minimized.

Copy link
Contributor

@jagoda jagoda commented Sep 28, 2015

This was considered in the original implementation (see #6). However, it's not as simple as it may seem. As the discussion in #6 indicates, the request object is usually available in the handler that is preparing the context so this is usually not necessary. Could you elaborate on your specific scenario to help me understand why you need this?

@g6123

This comment has been minimized.

Copy link
Author

@g6123 g6123 commented Sep 30, 2015

I'd like to make specific session data globally accessible in every template I use. So, now I'm using solution below, and it works well anyway.

server.ext('onPreResponse', function(request, reply) {
    var response = request.response; 

    // request.username has been set by other plugin
    var globalContext = {
        username: request.username
    };

    if(response.variety === 'view') {
        response.source.context = _.extend(
            globalContext, (response.source.context || {})
        );
    }

    reply(response);
});

I still hope vision supports this by cleaner way, but I understood why it's quite complicated.. Thanks.

@jagoda jagoda added this to the 4.1.0 milestone Mar 31, 2016
jagoda added a commit to jagoda/vision that referenced this issue Apr 1, 2016
jagoda added a commit to jagoda/vision that referenced this issue Apr 1, 2016
jagoda added a commit to jagoda/vision that referenced this issue Apr 1, 2016
jagoda added a commit to jagoda/vision that referenced this issue Apr 4, 2016
jagoda added a commit to jagoda/vision that referenced this issue Apr 4, 2016
@jagoda jagoda closed this in #81 Apr 4, 2016
@Marsup Marsup added feature and removed request labels Sep 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.