window-or-global

WebJar for window-or-global

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

window-or-global
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

window-or-global
WebJar for window-or-global
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/purposeindustries/window-or-global

Download window-or-global

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/window-or-global/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>window-or-global</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/window-or-global/
implementation 'org.webjars.npm:window-or-global:1.0.1'
// https://jarcasting.com/artifacts/org.webjars.npm/window-or-global/
implementation ("org.webjars.npm:window-or-global:1.0.1")
'org.webjars.npm:window-or-global:jar:1.0.1'
<dependency org="org.webjars.npm" name="window-or-global" rev="1.0.1">
  <artifact name="window-or-global" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='window-or-global', version='1.0.1')
)
libraryDependencies += "org.webjars.npm" % "window-or-global" % "1.0.1"
[org.webjars.npm/window-or-global "1.0.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

window-or-global

Use this module to get the global object both on server and client side. No more window is not defined errors just peace and flowers.

The code is borrowed from megawac/underscore@365311c.

Advantages:

  • You can easily mock it by mockery or proxyquire in your tests
  • It's really useful in case of universal (or isomorphic) code, for example, when you'd like to render a React component both on client and server side

Install

$ npm i window-or-global

Usage

import root from 'window-or-global'

React example

components/my-component.jsx:

import React, { Component } from 'react'
// in node, you'll get the global object instead of crashing by an error
import root from 'window-or-global'

class MyComponent extends Component {

  // this method is only invoked in the browser environment
  componentDidMount() {
    root.addEventListener(/*...*/)
  }

  componentWillUnmount() {
    root.removeEventListener(/*...*/)
  }

  render() {}

}

// Voilà. Enjoy your universal react component! ;)
// No more 'window is not defined' errors when you render your component
// on server side.

Unit-testing example

You can find a complete test example here. The test passes of course, just type

$ npm test

in your terminal.

License

MIT © Purpose Industries

org.webjars.npm

Purpose Industries

Versions

Version
1.0.1