1

I am trying to understand how to manage the scope in my Javascript code. Once I create a viewmodel, how do I access it from the rest of my javascript? (last line in this code generates a 'not defined' error

NOTE: I have found several posts on the differences between function/var for ko, but none have said there is a scoping advantage with using a var... so I have tried both methods.

//function AppViewModel() { // this.gridSize = ko.observable("30"); // this.canvasWidth = ko.observable("600"); // this.canvasHeight = ko.observable("600"); // this.displayCoords = "Axial"; // this.pixel="0"; // this.hex="0"; //} //ko.applyBindings(new AppViewModel()); var AppViewModel = { gridSize: ko.observable("30"), canvasWidth: ko.observable("600"), canvasHeight: ko.observable("600"), displayCoords: "Axial", pixel:"0", hex:"0" }; ko.applyBindings(AppViewModel ); var test = AppViewModel.gridSize; 
6
  • What is your question? Commented Aug 24, 2013 at 2:33
  • the line "var test = AppViewModel.gridSize" does not work, when stepping through says is undefined. Commented Aug 24, 2013 at 2:53
  • It works fine. We're going to need more information if there's still a problem. Commented Aug 24, 2013 at 2:57
  • Your problem is that you don't understand JavaScript, not that you don't understand Knockout. No biggie, but you need to understand how to reference what you are trying to before you do. Notice the differences between your commented code vs your view model... Commented Aug 24, 2013 at 3:22
  • yea the knockout is well documented and small, I am just stuck on Javascript. Obviously this is so simple that people think I am asking something harder. Commented Aug 24, 2013 at 4:02

1 Answer 1

1

ko.observable is a function and thus to reference a observable value you have to have brakets

e.g. from example above

var test = AppViewModel.gridSize(); 

found this good explanation http://knockoutjs.com/documentation/observables.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.