HTML and CSS Reference
In-Depth Information
quantity: newQuantity,
store: newStore
});
}
});
ViewModel.UserData.homeZipCode="NY 10118";
ViewModel.UserData.addStore("Whole Foods");
ViewModel.UserData.addStore("Kroger");
ViewModel.UserData.addStore("Costco");
ViewModel.UserData.addStore("Walmart");
ViewModel.UserData.addItem("Apples", 4, "Whole Foods");
ViewModel.UserData.addItem("Hotdogs", 12, "Costco");
ViewModel.UserData.addItem("Soda", "4 pack", "Costco");
})();
I'll return to the view model in a moment, but first I need to explain some of the other parts
of the code and the conventions they represent. I won't do this for subsequent files, but there is
some useful context to put in place as you get started with Metro development.
Using Code Completion
Visual Studio supports JavaScript code completion in the editor, which makes writing code sim-
pler and less error-prone. You must use a reference element to bring JavaScript files into scope
for code completion for files that are not in the local directory, like this:
/// <reference path="//Microsoft.WinJS.0.6/js/base.js" />
/// <reference path="//Microsoft.WinJS.0.6/js/ui.js" />
Prefixing the reference element with three slash ( / ) characters brings the reference to the
attention of Visual Studio and causes the JavaScript runtime to treat the statement like a com-
ment. With these additions, code-competition support for the WinJS API is added to the view-
model.js file. You need to add these reference elements to each of your JavaScript files if you
want WinJS completion.
Ti JavaScript code completion in the Visual Studio 11 beta is a little unreliable. I find that
trying to complete code often causes Visual Studio to crash—so much so that I have taken to
disabling code completion for JavaScript files in the Visual Studio options.
Reducing Global Namespace Pollution
One of the biggest problems in JavaScript is variable name collision. JavaScript variables
defined outside a function are globally available, and since there are only so many meaning-
ful variable names, it is only a matter of time before two different regions of code try to use the
same variable name for different purposes. Global variables are said to be part of the global
namespace , and defining global variables is often described as polluting the global namespace .
 
 
Search WWH ::




Custom Search