-
-
Save bradenpowers/1207520 to your computer and use it in GitHub Desktop.
Revisions
-
kwhinnery revised this gist
Sep 9, 2011 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ It is smarter in two ways: */ //monkey patch "require" in the global scope require('require_patch').monkeypatch(this); //regular modules are the same... var module = require('module'); File renamed without changes. -
kwhinnery revised this gist
Sep 9, 2011 . 4 changed files with 32 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ exports.Person = function(firstName,lastName) { this.firstName = firstName; this.lastName = lastName; }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ /* add a monkey-patched "require" function to the global scope (global object). It is smarter in two ways: - It only loads a module once - If the exports object contains a function matching the module base name, return that value from "require" - this is a bit of sugar added because Titanium's require implementation does not allow you to replace the "exports" object directly */ //monkey patch "require" in the global scope require('lib/require').monkeypatch(this); //regular modules are the same... var module = require('module'); module.sayHello('Marshall'); module.sayGoodbye('Kevin'); //modules which contain a type by the same name as the module... var Person = require('Person'); var jedi = new Person('Luke','Skywalker'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ exports.sayHello = function(name) { alert('Hello '+name+'!'); }; exports.sayGoodbye = function(name) { alert('Goodbye '+name+'!'); }; File renamed without changes. -
kwhinnery revised this gist
Sep 9, 2011 . 1 changed file with 13 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,17 @@ exports.monkeypatch = function(object) { var scriptRegistry = {}, old_require = object.require; object.require = function(moduleName) { if (!scriptRegistry[moduleName]) { var mod = old_require(moduleName), moduleRoot = moduleName.split(/[\/ ]+/).pop(); if (typeof(mod[moduleRoot]) === 'function') { scriptRegistry[moduleName] = mod[moduleRoot]; } else { scriptRegistry[moduleName] = mod; } } return scriptRegistry[moduleName]; }; }; -
kwhinnery created this gist
Jul 19, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ //This shim layer will not be necessary in future Titanium versions, but //for now will serve to only load a required module once per context. var require_once; (function() { var scriptRegistry = {}; require_once = function(moduleName) { if (!scriptRegistry[moduleName]) { scriptRegistry[moduleName] = require(moduleName); } return scriptRegistry[moduleName]; }; })();