Return-Path: X-Original-To: apmail-incubator-callback-dev-archive@minotaur.apache.org Delivered-To: apmail-incubator-callback-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9BD0D762B for ; Tue, 13 Dec 2011 19:56:44 +0000 (UTC) Received: (qmail 71657 invoked by uid 500); 13 Dec 2011 19:56:44 -0000 Delivered-To: apmail-incubator-callback-dev-archive@incubator.apache.org Received: (qmail 71632 invoked by uid 500); 13 Dec 2011 19:56:44 -0000 Mailing-List: contact callback-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@incubator.apache.org Delivered-To: mailing list callback-dev@incubator.apache.org Received: (qmail 71624 invoked by uid 99); 13 Dec 2011 19:56:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Dec 2011 19:56:44 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of jrburke@gmail.com designates 209.85.215.175 as permitted sender) Received: from [209.85.215.175] (HELO mail-ey0-f175.google.com) (209.85.215.175) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Dec 2011 19:56:36 +0000 Received: by eaal1 with SMTP id l1so49347eaa.6 for ; Tue, 13 Dec 2011 11:56:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=UNstotXXH0+6zps0OYyLmQQM6XudW+6viBkiKbGzCXw=; b=UhwTN1BCyoH8Rp31nnWHPk/sWapIN8sGCs62NOnk3SZ5My31+hffRE61OK/88O1dzv trmSquPgNeoOgVlfEEO1m0kZEnnuRNCRq3Rj3tQZnteL9E1lliyF5H/8WXEu5mUTytx8 K/pOR/CFBkXosQbU/b29VTMBTSYCNHAC2o4nY= MIME-Version: 1.0 Received: by 10.204.155.150 with SMTP id s22mr7710885bkw.140.1323806176401; Tue, 13 Dec 2011 11:56:16 -0800 (PST) Received: by 10.205.116.204 with HTTP; Tue, 13 Dec 2011 11:56:16 -0800 (PST) In-Reply-To: References: Date: Tue, 13 Dec 2011 11:56:16 -0800 Message-ID: Subject: Re: CommonJS and AMD (and, not or) From: James Burke To: callback-dev@incubator.apache.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Checked: Checked by ClamAV on apache.org On Tue, Dec 13, 2011 at 11:25 AM, Brian LeRoux wrote: > Looks rad. I'm curious what an AMD plugin vs the same plugin authored > in CJS style would look like. Just a hello world. Usually the only difference for coding a callback plugin in AMD or CJS would be a define wrapper in the AMD case: define(function(require, exports, module){ //CJS stuff goes in here. var PG = require('phonegap'); //to export a module value module.exports = {}; }); Most of the time, exports and module is not needed when using AMD, so you can just do: define(function(require){ var PG = require('phonegap'); //to set the module exports, return a value. //can be a function. return {}; }); AMD's define allows for imperative require use with a callback, so: define(function(require){ var mod = someCondition ? 'a' : 'b'; require([mod], function (modExports) { //called back asynchronously. }); }); This is really helpful for something like google maps code, where you may not want to load the code right away, but in response to a user action. This is difficult to express in CommonJS code, since require is always synchronous in that environment. Note though, that to get the dynamic require support for loading network resources a dynamic require loader, like RequireJS, curl, dojo, would be needed instead of almond. However, if the module was built into the built file containing almond, the above callback-style require will work with almond. James