Skip to content

Instantly share code, notes, and snippets.

@imaustink
Last active April 18, 2017 22:12
Show Gist options
  • Select an option

  • Save imaustink/d544f339f7e24978bdca6a65480ba5d1 to your computer and use it in GitHub Desktop.

Select an option

Save imaustink/d544f339f7e24978bdca6a65480ba5d1 to your computer and use it in GitHub Desktop.
Can globals examples

Examples

Setting Default Properties

const globals = require('can-globals');
// Set the global key's default value to the window object
globals.initialize('global', window);

Setting a dynamic Property

const globals = require('can-globals');
// To make the value dynamic you can set it to a function
globals.initialize("isRoot", function() {
  return window.location.pathname === "/";
});

Cached dynamic Property

const globals = require('can-globals');
// A third argument of true is passed to enable caching
// The function is called once and the returned value is cached for the next use
globals.initialize("isWebkit", function() {
  var div = document.createElement("div");
  return "WebkitTransition" in div.style;
}, true);

Setting a method-like Property

const globals = require('can-globals');

globals.initialize("isBrowserWindow", function() {
  return function(w) {
    return typeof window !== "undefined" &&
      typeof document !== "undefined" && typeof SimpleDOM === "undefined";
  };
});

Overwriting/Restoring Properties

const globals = require('can-globals');

globals.initialize('global', window);
// Overwrite global with an empty object
globals.global = {};
// Restore to default
delete globals.global;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment