I’m using KnockoutJS to display a list of users. On the same page a link triggers a shadowbox modal-window. When the user closes the modal I want the list of users to refresh. I already have the callback-function from the Shadowbox modal triggering, but how to make it refresh the list of users?
function User(data) {
this.firstname = ko.observable(data.firstname);
this.lastname = ko.observable(data.lastname);
this.fullname = ko.computed(function () {
return this.firstname() + " " + this.lastname();
}, this);
this.userfunctions = ko.observable(data.userfunctions);
this.phonework = ko.observable(data.phonework);
this.phonemobile = ko.observable(data.phonemobile);
this.email = ko.observable(data.email);
this.editurl = ko.observable(data.editurl);
}
function UserListViewModel() {
var self = this;
self.users = ko.observableArray([]);
self.users.loading = ko.observable(false);
self.users.loading(true);
$.getJSON("/UserJSON.ashx", {
requestType: "userList",
pageId: '12',
companyId: '1'
},
function (allData) {
var mappedUsers = $.map(allData, function (item) { return new User(item); });
self.users(mappedUsers);
self.users.loading(false);
});
}
ko.applyBindings(new UserListViewModel());
Have you looked at knockout mapping? It handles data updates from the server:
http://knockoutjs.com/documentation/plugins-mapping.html
I would create a “data” property on your viewmodel that gets loaded from knockout mapping, and can be updated from there. I’ll work on a jsfiddle and update this answer.
UPDATE
I was working on a JSFiddle, but found one that was already done 🙂 (Thanks RP Niemeyer!)
http://jsfiddle.net/rniemeyer/EDC7q/
UPDATE 2
I updated your JSFiddle: http://jsfiddle.net/sb2BX/1/
to this one: http://jsfiddle.net/JasonMore/sb2BX/3/
See it work, first click “Load Initial Data”, then click “Shadowbox Callback”. Does this answer your question?