I'm playing around with Knockout JS, and in this code sample:
var data =
{
allMakes: ko.observableArray([
{ name: "Toyota" },
{ name: "Fiat"}
]),
allModels: ko.observableArray([
{ name: "Corolla", make: "Toyota" },
{ name: "Celica", make: "Toyota" },
{ name: "Avensis", make: "Toyota" },
{ name: "Uno", make: "Fiat" },
{ name: "Bravo", make: "Fiat" }
])
};
var myViewModel =
{
makes : data.allMakes,
models: ko.computed(function() {
if (!myViewModel.selectedMake())
return;
// TODO: filter models by selected make
}),
selectedModel: ko.observable(""),
selectedMake: ko.observable("")
};
/* Uncomment it to work
myViewModel.models = ko.computed(function() {
if (!myViewModel.selectedMake())
return;
// TODO: filter models by selected make
});
*/
ko.applyBindings(myViewModel);
http://jsbin.com/upaxum/8/edit
we can see that I try to access myViewModel variable inside models: ko.computed . However myViewModel is undefined when this computed observable runs. And I don't know why?
However if I create models computed observable in next statement, myViewModel variable is defined. Why is that?
EDIT: one of the answers suggests that I'm accessing the object before it is created. So how come that this code snippet works?
var myViewModel =
{
myFnk : function()
{
console.log(myViewModel);
}
};
myViewModel.myFnk();