Skip to content

Instantly share code, notes, and snippets.

@Stuk
Stuk / README.md
Last active December 11, 2015 11:08
Montage app template

{{name}}

This is the Montage app template.

Note: Before working on your app you will need to add montage to it.

npm install .
var Serializer = require("montage/core/serialization").Serializer;
var MyComponent = Montage.create(Component, {
handleButtonClick: {
value: function (event) {
var serializer = Serializer.create().initWithRequire(require);
var serialization = serializer.serialize(this.theObjects);
console.log(serialization);
@Stuk
Stuk / promise-debug.js
Last active October 2, 2016 21:39
I've found this code useful for debugging promises, to find what order things are happening in. `console.error` gives a stack trace in the Webkit Developer Tools which makes easily narrow down where the promise creation and resolution is happening.
var Q = require("q");
var Set = require("collections/set"); // https://npmjs.org/package/collections
window.outstandingPromises = new Set();
var originalDefer = Q.defer;
Q.defer = function () {
console.error("Deferred created");
var deferred = originalDefer();
deferred.stack = new Error("").stack;
window.outstandingPromises.add(deferred);
@Stuk
Stuk / exec.js
Created August 14, 2013 00:15
Wrap Node's `child_process.spawn` with a promise interface that rejects if the process has an error, or exits with a code other than zero.
var spawn = require("child_process").spawn;
var Q = require("q");
/**
* Wrap executing a command in a promise
* @param {string} command command to execute
* @param {Array<string>} args Arguments to the command.
* @param {string} cwd The working directory to run the command in.
* @return {Promise} A promise for the completion of the command.
*/
@Stuk
Stuk / index.js
Created October 7, 2013 01:14
Parsing POST requests in Joey
var joey = require("joey");
// Uses qs https://npmjs.org/package/qs
var qs = require("qs");
joey
.log()
.error()
.favicon()
.route(function ($) {
var defaultLocalizer = require("montage/core/localizer").defaultLocalizer;
defaultLocalizer.localize("hello_name", "Hello, {name}!")
.then(function (hi) {
console.log(hi({name: "World"})); // => "Hello, World!", or the translation at the "hello_name" key
});
defaultLocalizer.localize("cat", "Cat")
.then(function (cat) {
console.log(cat.toString()); // => "Cat", or the translation at the "cat" key
@Stuk
Stuk / index.html
Created November 8, 2013 18:39
Using montage with deep urls
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>resolve</title>
<!-- Base URL ensures montage.js and style.css are loaded from the root of the server
Make sure this comes before any `script`, `style`, or `link` tags to ensure that
resources are loaded from the correct place. -->
<base href="/">
@Stuk
Stuk / heap-require.js
Created April 7, 2014 20:55
A little module that can reveal if memory usage jumps after requiring modules
// usage:
// require = require("./heap-require")(require);
// then use require as normal
module.exports = function (_require) {
var newRequire = function (id) {
var before = process.memoryUsage().heapUsed / 1024 / 1024;
var exports = _require(id);
var after = process.memoryUsage().heapUsed / 1024 / 1024;
var webpackConfig = {
plugins: [
// Plugin to show any webpack warnings and prevent tests from running
function () {
this.plugin("done", function (stats) {
if (stats.compilation.warnings.length) {
// Log each of the warnings
stats.compilation.warnings.forEach(function (warning) {
console.log(warning.message || warning);
});