While going through the examples of KnockoutJS, I saw the below code.
function WebmailViewModel() {
// Data
var self = this;
self.folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
self.chosenFolderId = ko.observable();
// Behaviours
self.goToFolder = function(folder) { self.chosenFolderId(folder); };
};
ko.applyBindings(new WebmailViewModel());
I am not an expert in Javascript, but bit confused by the usage self.chosenFolderId(folder);
chosenFolderId is a property, and assigned ko.observable(); From the experience with other languages,
- How can one invoke it by passing an argument like
self.chosenFolderId(folder); - Where is folder defines?
If you can just point to an article which explains this that will do.
Thanks.
chosenFolderIdis a property, but properties may be functions (and must be, in this case).So
ko.observablereturns a function that takes a single argument (the folder).It’s no different than the next line:
where the
goToFolderproperty is being set to a function.folderitself is “defined” as a parameter asgoToFolder‘s parameter. Whatever callsgoToFolderprovides a value forfolder.