The Setup (Config) File
This file contains 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 characters
requirejs.config({ | |
baseUrl: "Scripts", | |
paths: { | |
app: "../App", | |
jquery: "jquery-2.1.4", | |
knockout: "knockout-3.3.0.debug", | |
bootstrap: "bootstrap" | |
}, | |
shim: { | |
"bootstrap": ["jquery"], | |
"knockout": ["jquery"] | |
} | |
}); | |
requirejs(["app/something/somedependency", "app/authentication/someotherdependency"]); |
Notice that the JavaScript files do not have the .js extension in the config file. The shim property is used to ensure certain load orders (Bootstrap requires jQuery). The paths can be combined when loading dependencies.
The HTML File
This file contains 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 characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Hello, World!</title> | |
<link rel="stylesheet" href="~/Content/bootstrap.css" /> | |
<link rel="stylesheet" href="~/Content/bootstrap-theme.css" /> | |
</head> | |
<body> | |
<script type="text/javascript" data-main="App/config" src="~/Scripts/require.js"></script> | |
</body> | |
</html> |
It still needs the css files. The data-main attribute in the scripts tag tells RequireJS to get everything from that file.
Modules
It's not necessary to have a parameter for each dependency. The above module uses a bootstrap modal function, but doesn't need a variable (lines 34 and 55).
Examples of getting and posting some json are at lines 24 and 48. I use the $.ajax at 48, because it lets me specify the contentType. I ran into problems using $.post.
More to come on this...
This file contains 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 characters
define(["jquery", "knockout", "bootstrap"], function($, ko) { | |
var lessons = function() { | |
// ReSharper disable once InconsistentNaming | |
var bus = new ko.subscribable(); | |
l.Bus = bus; | |
var person = function(firstName, lastName) { | |
var self = this; | |
self.firstName = ko.observable(firstName); | |
self.lastName = ko.observable(lastName); | |
}; | |
var peopleListViewModel = function() { | |
var self = this; | |
bus.subscribe(function() { | |
self.loadPeople(); | |
}, self, "Person Added"); | |
self.people = ko.observableArray([]); | |
self.loadPeople = function() { | |
$.getJSON("/people", "", function(data) { | |
self.people.removeAll(); | |
$.each(data, function(key, value) { | |
self.people.push(new person(value.FirstName, value.LastName)); | |
}); | |
}); | |
}; | |
self.showAddPerson = function() { | |
$("#addPersonModal").modal("show"); | |
}; | |
}; | |
l.PeopleListViewModel = peopleListViewModel; | |
var addPersonViewModel = function() { | |
var self = this; | |
self.firstName = ko.observable(""); | |
self.lastName = ko.observable(""); | |
self.addPerson = function() { | |
var personToAdd = ko.toJSON(self); | |
$.ajax({ | |
method: "POST", | |
url: "/people", | |
data: personToAdd, | |
contentType: "application/json; charset=utf-8", | |
success: function() { | |
bus.notifySubscribers({}, "Person Added"); | |
$("#addPersonModal").modal("hide"); | |
self.firstName(""); | |
self.lastName(""); | |
}, | |
}); | |
}; | |
}; | |
l.AddPersonViewModel = addPersonViewModel; | |
}; | |
return lessons; | |
}); |
It's not necessary to have a parameter for each dependency. The above module uses a bootstrap modal function, but doesn't need a variable (lines 34 and 55).
Examples of getting and posting some json are at lines 24 and 48. I use the $.ajax at 48, because it lets me specify the contentType. I ran into problems using $.post.
More to come on this...