Skip to content

Instantly share code, notes, and snippets.

@caloggins
Created July 9, 2015 13:00
Some different things with a module in RequireJS.
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;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment