Skip to content

Instantly share code, notes, and snippets.

@dipakc
Last active July 16, 2017 08:20
Show Gist options
  • Select an option

  • Save dipakc/09898ca7251aed63a8401e557e254798 to your computer and use it in GitHub Desktop.

Select an option

Save dipakc/09898ca7251aed63a8401e557e254798 to your computer and use it in GitHub Desktop.

File structure

├── app.js
├── index.html
├── lib
│   ├── modules
│   │   └── template.js
│   ├── require.js
│   └── underscore.js

index.html

<html>
   <head>
      <script src="lib/require.js" data-main="app"></script>
   </head>
   
   <body>
      <h1> RequireJS Sample Page </h1>
   </body>
</html>

app.js

require.config({
  paths: {
    "jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min", //no js extension
    "underscore": "lib/underscore",
  }
});

require(['lib/modules/template'], function(template) {
  template.showName("Jack");
});

template.js

define(['underscore', 'jquery'], function() {
  var showName = function(n) {
    var temp = _.template("Hello <%= name %>");
    $("body").html(temp({name: n}));
  };
  return {
    showName: showName
  };
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment