Thursday, July 9, 2015

SPA Notes

This is an outline of some stuff for weaving KnockoutJS, RequireJS, and Bootstrap into a site. I'm not saying these are best practices or anything. Just my notes on what I have been doing. Mostly geared towards SPAs.

The Setup (Config) File

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"]);
view raw config.js hosted with ❤ by GitHub

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

<!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>
view raw index.html hosted with ❤ by GitHub

It still needs the css files. The data-main attribute in the scripts tag tells RequireJS to get everything from that file.

Modules

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;
});
view raw people.js hosted with ❤ by GitHub


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...