Examples

Add Soup Support

The minimal newtApp recipe doesn't provide any support for indexes, correct soup name, or for initial values of slots. The books are even displayed in random order. Let's modify the Bookstore application to fix this. A QuickTime movie of this example is available. We will have an index on all of these slots:

We may want to sort on any of these values. In addition, let's use a unique soup name (see "Creating Unique Application Symbols and Names" on page 419). Finally, let's provide initial values for slots. In order to do this, modify the allSoups slot of the newtApplication template to:

{
   mySoup: {
      _proto: newtSoup,
      soupName: "Bookstore:Calliope",
      soupIndices: [
         {
            structure: 'slot,
            path: 'author,
            type: 'string,
         },
         {
            structure: 'slot,
            path: 'title,
            type: 'string,
         },
         {
            structure: 'slot,
            path: 'acquireDate,
            type: 'int,
         },
         {
            structure: 'slot,
            path: 'numberInStock,
            type: 'int,
         },
      ],
      soupQuery: {indexPath: 'author},
      CreateBlankEntry: func()
      begin
         return {
            author: "",
            title: "",
            acquireDate: Time(), // today!
            numberInStock: 1,
            price: 15.95, // most common price
         };
      end,
   },
}
It is very important to create a CreateBlankEntry along with our soupQuery. If CreateBlankEntry were not present, then when the application opens, a blank entry with no slots would be created. When this entry was added to the soup, it wouldn't be added to the author index, as there would be no author slot. The cursor that used the author index would then skip this blank entry. Now, the default layout would get in deep trouble-- it would be trying to edit an entry that wasn't part of the cursor. This trouble is completely avoided if our CreateBlankEntry ensures that all slots necessary for our indexes are filled out in advance.

When we test our application, we find that as new entries are created (by initially opening the application, or by switching to an empty folder), they have reasonable default values. In addition, entries are sorted by author.


Note:If you modify the soupIndices array after you've created the soup, you'll get this error:
Key does not have the type specified in the index
evt.ex.fr.store
-48019
Once a soup has been created it maintains its original index(es). Even if you update the soupIndices slot in allSoups and rerun your application, it won't add new indexes. The soupIndices is used only when a soup is created.
Anytime you change the soup indexes, you need to recreate the soup. The easiest way to do that is to delete the soup (scrub it from the Storage folder in the Extras drawer). Now when you run the application it will create a new soup with the proper indexes.


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996