Getting confused by stupid javascript again
-
I've been working on an app I've mentioned/asked questions about. I got stuck again. I am having a weird problem where the code I wrote won't work if I call it in the console, but it works if I actually type it all out in the console.
Here's the relevant code:
self.app.state.clients.client = new ItemsState({ accessor: function(obj) { return obj.client.id(); } , saveItem: function(item) { var self = this; var person = ko.toJSON(item.person); if (person.id) { //record exists in the database, so we're updating by key $.post('/api/person/' + person.id, person, function(resp) { console.log(resp); self.item().person = resp; }); } } });
Specifically, I'm calling the
saveItem
function in the console:console> var item = viewModel.app.state.clients.client.item() undefined console> item {itsClient: {…}, person: {…}, address: {…}, demographics: {…}, client: {…}, …} console> viewModel.app.state.clients.client.saveItem(item) undefined
So it runs, but the
$.post
never gets triggered. I don't see a request at the server. I have made sure thatperson.id
is defined:console> ko.toJSON(item) "{"person":{"middleName":null,"preferredFirstName":"Jaysen","familyName":"Bateman","preferedLastName":null,"firstName":"Jason","preferedMiddleName":null,"id":2},}
(I edited this value to get rid of irrelevant fields, so it might not be valid json anymore)
What am I missing? Something obvious and dumb, I'm sure.
-
This post is deleted!
-
@captain Yes, it was dumb and obvious. Obviously,
person
stored a string, soperson.id
was undefined...