GithubHelp home page GithubHelp logo

isabella232 / meteor-publish-context Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peerlibrary/meteor-publish-context

0.0 0.0 0.0 7 KB

Provide DDP._CurrentPublish with access to current publish context

Home Page: https://atmospherejs.com/peerlibrary/publish-context

License: BSD 3-Clause "New" or "Revised" License

JavaScript 53.74% CoffeeScript 46.26%

meteor-publish-context's Introduction

DDP._CurrentPublish with current publish context

Adding this package to your Meteor application creates a DDP._CurrentPublish environment variable which you can use to access current publish context (this inside publish function) from anywhere in the server-side code, if that server-side code was called from a publish function.

It is similar to existing undocumented DDP._CurrentInvocation which does the same for method calls, giving you access to the current context of a method call (this inside a method's body).

Not needed with Meteor >= 1.5.1. There is DDP._CurrentPublicationInvocation now available, and DDP._CurrentInvocation has been renamed to DDP._CurrentMethodInvocation. This package provides the same functionality for previous Meteor versions.

Server side only.

Installation

meteor add peerlibrary:publish-context

After adding the package, you have to depend on, or import, Meteor's core ddp package to get access to the DDP symbol.

Examples

Implementing a userId() function on the server-side, which works in any code called from methods or publish functions, can then be simply implemented as:

function userId() {
  const currentInvocation = DDP._CurrentInvocation.get();
  if (currentInvocation) return currentInvocation.userId;

  const currentContext = DDP._CurrentPublish.get();
  if (currentContext) return currentContext.userId;

  throw new Error("userId() not invoked from a method or publish function.");
}

This is more or less what peerlibrary:user-extra package does to provide Meteor.userId().

meteor-publish-context's People

Contributors

mitar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.