Here's the JSFiddle.
The array of objects looks like below:
var data = [{
"ConferenceUsers": [{
"Id": 3006,
"ConferenceId": 8,
"Name": null,
"Email": "mail@lala.com",
"UserName": null,
"PhoneNumber": "234234234234"
}],
"Id": 8,
"Subject": "YYYhaaaaa",
"StartTime": "2016-05-29T18:30:00",
"EndTime": "2016-05-29T19:30:00",
"OrganizerEmail": "elpas@live.com",
"OrganizerName": "dasdasd",
}, {
"ConferenceUsers": [{
"Id": 3013,
"ConferenceId": 12,
"Name": null,
"Email": "dsfdfsdfdsf@dfdfdf.com",
"UserName": null,
"PhoneNumber": null
}],
"Id": 12,
"Subject": "dsfsdfdsfsdf",
"StartTime": "2016-05-31T22:00:00",
"EndTime": "2016-05-31T23:00:00",
"OrganizerEmail": "d@adssad.com",
"OrganizerName": "dsfdsfsdf"
}];
I need to be able to add and edit new ConferenceUsers
.
Adding new users works but I can't edit them.
ConferenceUser ViewModel:
var ConferenceUser = function (user) {
this.ConferenceId = ko.observable(user.ConferenceId);
this.Email = ko.observable(user.Email);
this.Id = ko.observable(user.Id);
this.Name = ko.observable(user.Name);
this.PhoneNumber = ko.observable(user.PhoneNumber);
};
ConferenceList ViewModel and mappings:
var createConferenceUser = function (user) {
return new ConferenceUser(user);
};
var ConferenceList = function(conferencesJSON) {
var self = this;
var users = [];
for (i = 0; i < conferencesJSON.length; i++) {
users.push(conferencesJSON[i].ConferenceUsers);
}
this.conferences = ko.observableArray(conferencesJSON.map(createConference));
this.conferenceUsers = ko.observableArray(users.map(createConferenceUser));
this.addConference = function(conferenceJSON) {
self.conferences.push(createConference(conferenceJSON));
};
};
ko.applyBindings(new ConferenceList(data));
Q: How can I update the existing / newly added ConferenceUsers
and the DOM also, like in the jsFiddle?
Like I commented: if you were to actually use the
ConferenceUser
view models with the observable properties, you would probable able to figure out how to both create new, and edit existing users. I do see some other points of improvement though, so I think it's still worth to formulate an answer to your question:Conference
view model and can easily be separated into its own little widgetI've written an answer that's more about the architecture because I feel that the answer from your question follows naturally from a well defined separation of concerns. And because I enjoy refactoring :)
Here's my suggestion after having moved stuff around. You can decide for yourself how much of it you'll use, but at least the answer to your question can be found in the code!
ConferenceList
Only serves to manage a list of conferences. Could easily be extended with
removeConference
orgetConferencesFromServer
etc.Conference
Holds a list of
ConferenceUser
instances and a widget to edit and create new users. This is where your question is answered:ConferenceUser
This is where we make sure the UI is updated after edits: note that the
Email
andPhoneNumber
properties are observable. I didn't create observables for all properties to indicate not all properties are meant to be changed in the UI.I've created a static create method for two reasons:
id
inside a closure to make sure it's uniqueArray.prototype.map
method.The code:
UserEditor
This is the biggest improvement (I believe) to your code: an editor widget that helps you create, edit and save new users. The methods it exposes are much easier to understand and write because they're not inside your
Conference
viewmodel.Now, after all this code, you'll see your HTML is very straight forward. Here's your edit widget:
Kind of a long story to get to the fix, but if you've made it this far: here's an updated fiddle! https://jsfiddle.net/e2ox4doj/