Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@EndangeredMassa
Created October 23, 2012 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EndangeredMassa/defd3844f98fe126f196 to your computer and use it in GitHub Desktop.
Save EndangeredMassa/defd3844f98fe126f196 to your computer and use it in GitHub Desktop.
Modules and Dependency Management

Sean Massa

@endangeredmassa

Chicago Node.js Meetup

Global Scope

function processOrder(itemId, quantity, price) {
  // process the order
}

Object Scope

App = {}
App.Orders = {}
App.Orders.process = function(itemId, quantity, price) {
  // process the order
}

Function Scope and the Module Pattern

Orders = (function(){
  // new scope!

  function process(itemId, quantity, price) {
    // process the order
  }

  return {
    process: process
  }
})()

Dependencies

App.Orders = (function(){
  function process(itemId, quantity, price) {
    App.Api.createOrder(itemId, quantity * price)
  }

  return {
    process: process
  }
})()

Alias Method

// dependencies
var Api = App.Api

App.Orders = (function(){
  function process(itemId, quantity, price) {
    Api.createOrder(itemId, quantity * price)
  }

  return {
    process: process
  }
})()

jQuery Method

App.Orders = (function(){
  function createOrderForm() {
    // create order form in $order
    return $order
  }

  function newOrder() {
    $order = createOrderForm()
    $order.Tooltip(options)
  }

  return {
    create: newOrder
  }
})()

jQuery Method (fixed?)

Tooltip = ???

App.Orders = (function(){
  function createOrderForm() {
    // create order form in $order
    return $order
  }

  function newOrder() {
    $order = createOrderForm()
    Tooltip.create($order, options)
  }

  return {
    create: newOrder
  }
})()

ender

http://ender.no.de

// command line
$ ender add domReady

// <script src="ender.min.js"></script> 

ender ($ api)

$.domReady(function () {
  // do some work
})

bower

http://twitter.github.com/bower

// command line
$ bower install jquery

// <script src="components/jquery/index.js"></script>

emport

https://github.com/quackingduck/emport

#@export Orders

#@import $
#@import $.fn.chosen
#@import underscore

Orders = (function(){
  // implementation
})

requirejs and AMD

http://requirejs.org

// orders.js
define(['underscore'], function(_){
  // ...
  return {
    process: process
  }
})

require(['jquery', 'orders'], function ($, Orders) {
  // use $ and Orders here
})

ender (require api)

// script
var backbone = require('backbone')
  , _ = require('underscore')

backbone.Models(...)
_.each(...)

browserify and CommonJS

https://github.com/substack/node-browserify

// order.js
var $ = require('jquery')
// ...
module.exports = {
  process: process
}

// npm install domready

var Orders = require('./orders')
var domready = require('domready')

domready(function () {
    var newOrder = Orders.create()
})

Thanks!

@endangeredmassa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment