I have a knockout script that loads the data from the server via ajax and outputs this information into the model. If to simplify everything, my model looks like this:
function ArticleViewModel() {
var self = this;
this.articleInfo = ko.observable();
this.getArticle = function(id) {
$.get("/router.php", { articleId: id }, self.articleInfo, 'json');
};
};
Everything works nicely (my view is populated with an information from the server). But what I need is to modify this information (for example change the data from timestamp to human readable format).
As I understood it, one way of achieving it is through knockout computed observables, but I do not see a reason of doing this because I will never need timestamp here and I can just change my data once after update and use it (if I am wrong, I would be happy to hear why and how to achieve what I want with computed observables).
So I am trying to change my data on ajax request in the following way (everything is the same, but ajax call has a callback):
$.get("/router.php", { type : 'mail' }, function(i){
var d = new Date(i.date);
self.articleInfo = {
date: d.toString(),
title: i.title
};
}, 'json');
I have no errors on the page, but no information shows up. When I go to some other page, I see a warning: TypeError {stack: (...), message: "500 Error get /#Home Property 'articleInfo' of object #<ArticleViewModel> is not a function"}
What am I doing wrong?