Skip to content

Instantly share code, notes, and snippets.

@domenic
domenic / di-in-requirejs.js
Created October 10, 2011 04:05
Dependency injection sample with RequireJS
// EntryPoint.js
define(function () {
return function EntryPoint(model1, model2) {
// stuff
};
});
// Model1.js
define(function () {
return function Model1() {
@domenic
domenic / di-plus-sl-modules.js
Last active September 27, 2015 13:17
Intermediate DI/service locator solution for JavaScript modules
//////////////////////////
// Pure "DI style" modules
// BackEndAnalytics.js
define(function () {
return function BackEndAnalytics() {
this.send = function () { };
};
});
@domenic
domenic / promise-assertions.js
Created November 10, 2011 23:26
Sample of upcoming promise assertions module
// Say that doSomethingAsync returns a promise, and expected is the value we want it to return.
// With my library you can do the following
test("doSomethingAsync() returns expected", function () {
equal(doSomethingAsync(), expected, "As expected");
});
// Normally you would have to do
test("doSomethingAsync() returns expected", function () {
QUnit.stop();
@domenic
domenic / gist:1370488
Created November 16, 2011 16:08
Discussion of AMD vs. CommonJS Modules/2.0

Just want to chime in that I'm glad people are still interested in CommonJS Modules/2.0. We have a working implementation that hopefully will be of interest, and have factored out the generally applicable parts of our test suite. Also see the announcement thread on the CommonJS list.

That said, personally I've been a bit disappointed with the CommonJS community's lack of enthusiasm on Modules/2.0. We picked it up early in our project lifecycle, and ran with it full speed. At the time it seemed like a more standardized alternative to AMD.

However, over time it's become clear that AMD has not only won mindshare but also has a much more active group of maintainers and a better, more egalitarian standards process. Whereas, Modules/2.0 has only the perennially busy Wes Garland, leaving a self-hosted PDF s

@domenic
domenic / user.dic
Created November 18, 2011 03:30
Math and science words that need to be in dictionaries but usually aren't
BPS
CKM
CMB
CSS
CVS
KMS
LHC
LQG
LSZ
NSF
@domenic
domenic / cancellable-deferreds.js
Created December 20, 2011 04:24
Cancellable deferreds
var deferredsByPromise = new WeakMap();
recordRetriever.retrieveAsync = function (query) {
var deferred = Q.defer();
server.doPagedRetrieveWithCallback(query, 0, function (err, result) {
// wire up deferred.resolve and deferred.reject.
// recurse appropriately, increasing pagination,
// UNLESS deferred.isCancelled.
});
@domenic
domenic / cancellable-promises.js
Created December 20, 2011 04:27
Cancellable promises
recordRetriever.retrieveAsync = function (query) {
var deferred = Q.defer();
server.doPagedRetrieveWithCallback(query, 0, function (err, result) {
// wire up deferred.resolve and deferred.reject.
// recurse appropriately, increasing pagination,
// UNLESS deferred.isCancelled.
});
deferred.promise.cancel = deferred.cancel;
@domenic
domenic / README.md
Last active June 24, 2021 16:37
Using promises in a UI context

The scenario:

  • We are writing a digital textbook-reading app.
  • Most of the time you have a "basic" license for your textbook, but one (and only one) of your computers can request an "enhanced" license.
  • You can only print from the computer with the enhanced license.

The problem statement:

@domenic
domenic / q-bind.js
Created February 9, 2012 20:20
Q.bind
// From discussion at https://groups.google.com/d/msg/q-continuum/68ro20aB358/7kUCAF19qqAJ
// Naive version
function fAsync(a, b) {
if (a) {
return Q.reject(new Error());
}
if (b) {
return Q.resolve(42);
}
@domenic
domenic / knockout-es5.js
Created February 10, 2012 21:51
Speculations on an ES5-style KnockoutJS
// DESIRED (pending bikeshedding):
// Create using special factory function. Will automatically create `ko.observable`s, `ko.observableArray`s, and
// `ko.computed`s for you, but hide them behind getters/setters.
var viewModel = es5ViewModel({
firstName: "Luke",
lastName: "Skywalker",
fullName: function () {
return this.fullName + this.lastName;
},