concept birthdate property in category nativescript

appears as: birthDate property
NativeScript in Action

This is an excerpt from Manning's book NativeScript in Action.

Looking at our example, the birthDate property of our data-bound observable is a date, and the text property of the text field displays a string. On its own, NativeScript data binding doesn’t know how to convert between two different objects. It’ll guess, but often it’s wrong. Think about it for a minute: there are dozens of ways to display a date: US format (MM/DD/YYYY or MM/DD/YY), ISO format (YYYY-MM-DD), and so on. To solve this problem, you create a converter function that explicitly describes how to convert the values.

Listing 11.5 Updated savePage() fileSystemService.js to support databinding to the birthDate property
fileSystemService.prototype.getPages = function () { var pages = []; if (this.file.readTextSync().length !== 0) { pages = JSON.parse(this.file.readTextSync()); } pages.forEach(function (page) { page.birthDate = new Date(page.birthDate); #1 if (page.imageBase64 != null) { page.image = imageModule.fromBase64(page.imageBase64); } }); return pages; } fileSystemService.prototype.savePage = function (scrapbookPage) { var pages = this.getPages(); var index = pages.findIndex(function (element) { return element.id === scrapbookPage.id; }); if (index !== -1) { pages[index] = { id: scrapbookPage.id, title: scrapbookPage.title, gender: scrapbookPage.gender, birthDate: scrapbookPage.birthDate, imageBase64: scrapbookPage.image != null ? scrapbookPage.image.toBase64String("png") : null, lat: scrapbookPage.lat, long: scrapbookPage.long }; } else { pages.push({ id: scrapbookPage.id, title: scrapbookPage.title, gender: scrapbookPage.gender, birthDate: scrapbookPage.birthDate, imageBase64: scrapbookPage.image != null ? scrapbookPage.image.toBase64String("png") : null, lat: scrapbookPage.lat, long: scrapbookPage.long }); } var json = JSON.stringify(pages); this.file.writeText(json); }; #1 create a new Date object when date is read from the file