js/app.js
// js is the root dir for all js files
// thrid party libaries are stored in lib folder
// custom js modules are stored in app folder
// shim handlebars because it doesn't support amd yet
requirejs.config({
baseUrl: 'js',
paths: {
app: 'app',
lib: 'lib',
jquery: "http://code.jquery.com/jquery-1.11.2.min",
underscore: "lib/underscore",
handlebars: "lib/handlebars"
},
shim: {
handlebars: {
exports: "handlebars"
}
}
});
// Start loading the main app file.
requirejs(['app/main']);
js/app/main.js
define(function (require) {
var hello = require('app/helloworld');
hello.narrator();
hello.greeting();
hello.msg();
require(['app/template'], function(template) {
template.underscoreTemplate("underscore");
template.handlebarsTemplate({name:"Handlebars"});
});
});
js/app/helloworld.js
define(['jquery'], function () {
var hello = {
narrator: function() {
console.log('A new requirejs user says: ');
$('#my-msg').append('A new requirejs user says:');
}
};
hello.greeting = function () {
console.log('Hello World!');
$('#my-msg').append('Hello World!');
};
(function () {
this.msg = function() {
console.log('This is my first practice of using requirejs!');
$('#my-msg').append('This is my first practice of using requirejs!');
};
}).apply(hello);
return hello;
});
js/app/template.js
define(['jquery', 'underscore', 'handlebars'], function($, _, Handlebars) {
var greeting = function(n) {
var temp = _.template("Hello <%= name %>");
$("#underscroeTemplate").html(temp({name: n}));
};
var greeting2 = function(n) {
var temp = Handlebars.compile('Hello {{name}}');
$("#handlebarsTemplate").html(temp(n));
}
return {
underscoreTemplate: greeting,
handlebarsTemplate: greeting2
};
});