"The NativeScript Book" by Branstein brothers Errata?


#21
var observableArray = require("data/observable-array");
var frame = require("ui/frame");
var fileSystemService = require("~/data/fileSystemService");

function scrapbookPageModel() {
  // #A
  var model = new observable.fromObject({
    id: id,
    genders: [
      "Female", "Male", "Other"
    ],
    title: "",

    gender: "",
    calcAge: function(birthDate) {
      var now = Date.now();
      var diff = Math.abs(now - birthDate) / 1000 / 31536000;
      return diff.toFixed(1);
    }
  });

  return model;
}

exports.onLoaded = function(args) {
  var page = args.object;
  var scrapbook = new observable.fromObject({pages: new observableArray.ObservableArray()});
  var pages = fileSystemService.fileSystemService.getPages();

  if (pages.length !== 0) {
    pages.forEach(function(item) {
      var model = new scrapbookPageModel(item.id); // #B
      model.title = item.title;
      model.gender = item.gender;
      model.year = item.year;
      model.month = item.month;
      model.day = item.day;
      scrapbook.pages.push(model);
    });
  }
  page.bindingContext = scrapbook;
};

exports.onAddTap = function(args) {
  var page = args.object;
  var scrapbook = page.bindingContext;
   scrapbook.pages.push(new scrapbookPageModel());  //  this was missing
  frame.topmost().navigate({
    moduleName: "views/scrapbookUpdate-page",
    context: {
      model: new scrapbookPageModel(scrapbook.pages.length)
    } // #A
  });
};
exports.onItemTap = function(args) {
  var page = args.object;
  var scrapbook = page.bindingContext;

 
  frame.topmost().navigate({
    moduleName: "views/scrapbookUpdate-page",
    context: {
      model: scrapbook.pages.getItem(args.index)
    } // #B
  });
};

formatted fileSystemService.js

var fileSystem = require("file-system"); // #A
var fileSystemService = function() {
  this.file = fileSystem.knownFolders.documents().getFile("scrapbook.json");
};
fileSystemService.prototype.getPages = function() {
  // #B
  var pages = [];
  if (this.file.readTextSync().length !== 0) {
    pages = JSON.parse(this.file.readTextSync()); // #C
  }
  return pages;
};
fileSystemService.prototype.savePage = function(scrapbookPage) {
  // #D
  var pages = this.getPages();
  var index = pages.findIndex(function(element) {
    // #E
    return element.id === scrapbookPage.id;
  });
  if (index !== -1) {
    pages[index] = {
      id: scrapbookPage.id,
      title: scrapbookPage.title,
      gender: scrapbookPage.gender,
      year: scrapbookPage.year,
      month: scrapbookPage.month,
      day: scrapbookPage.day
    };
  } else {
    pages.push({
      id: scrapbookPage.id,
      title: scrapbookPage.title,
      gender: scrapbookPage.gender,
      year: scrapbookPage.year,
      month: scrapbookPage.month,
      day: scrapbookPage.day
    });
  }
  var json = JSON.stringify(pages); // #F
  this.file.writeText(json); // #F
};

You forgot to export the file service

exports.fileSystemService = new fileSystemService();

This is a common mistake so any time you have an undefined object check this first. :smiley:


#22

Please make sure you use code formatting with the 3 backticks. I had a heck of a time getting the formatting back the way it should be because of inserted curly quotes and what not. Just something to be aware of. You used block quote or something I think.


#23

D’oh! Thanks for taking the time to spot that. Really appreciate it.

I’ll take note to use the 3 backticks when posting in future…and check this first on the undefined object!


#24

I do it all the time. catwood2


#25

Hi. Someone help me with this please. I’m trying to follow along with the book but cant figure out how to carry out calculations based on the age.
`function onTap(year, month, day)
{
var date = new Date(year, month, day);
var now = Date.now();
var diff = Math.abs(now - date) / 1000 / 31536000;

  var age = diff.toFixed(1);
}`

I am trying to get the age on tap and set the age to the variable age.


#26

Plz I’m getting this error message when using the nativescript book in page 206 scrapbook -page. Js listing 8.20