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