ui-ts-building-data-model.md 2.8 KB
Newer Older
G
ge-yafang 已提交
1
# Building a Food Data Model
Z
zengyawen 已提交
2 3


G
ge-yafang 已提交
4
On the created page, we use various items to describe food, such as food names, calories, proteins, fats, carbohydrates, and vitamin C. This form of coding is impractical in actual development. Therefore, you need to create food data models to store and manage data in a unified manner.
Z
zengyawen 已提交
5 6


G
ge-yafang 已提交
7 8 9 10 11 12 13 14 15
![en-us_image_0000001267767897](figures/en-us_image_0000001267767897.png)


1. Create a folder named model and create a file named FoodData.ets therein.
   ![en-us_image_0000001223127760](figures/en-us_image_0000001223127760.png)

2. Define a food data storage model, FoodData, and an enum variable, Category. The FoodData class contains the food ID, name, category, image, calories, protein, fat, carbohydrates, and vitamin C attributes.
   The eTS programming language is an extension of the TS language and also supports the TS syntax.

H
HelloCrease 已提交
16
   
G
ge-yafang 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
   ```
   enum Category  {
     Fruit,
     Vegetable,
     Nut,
     Seafood,
     Dessert
   }
   
   let NextId = 0;
   class FoodData {
     id: string;
     name: string;
     image: Resource;
     category: Category;
     calories: number;
     protein: number;
     fat: number;
     carbohydrates: number;
     vitaminC: number;
   
     constructor(name: string, image: Resource, category: Category, calories: number, protein: number, fat: number, carbohydrates: number, vitaminC: number) {
       this.id = `${ NextId++ }`;
       this.name = name;
       this.image = image;
       this.category = category;
       this.calories = calories;
       this.protein = protein;
       this.fat = fat;
       this.carbohydrates = carbohydrates;
       this.vitaminC = vitaminC;
     }
   }
   ```

3. Store food image resources in the resources > phone > media directory. Use food names as the image names.
H
HelloCrease 已提交
53
   
G
ge-yafang 已提交
54
4. Create food resource data. Create FoodDataModels.ets in the model folder and declare a food composition array, FoodComposition on the page.
H
HelloCrease 已提交
55
   In this example, you can customize more data resources when needed. Use LazyForEach to load data if a large amount of food data is involved. 
G
ge-yafang 已提交
56 57

5. Create the initializeOnStartUp method to initialize the FoodData array. Export the FoodData class from FoodData.ets, and import FoodData and Category in FoodDataModels.ets.
H
HelloCrease 已提交
58
  
G
ge-yafang 已提交
59 60 61 62 63 64
   ```
   // FoodData.ets
   export enum Category {
    ......
   }
   export class FoodData {
Z
zengyawen 已提交
65
     ......
G
ge-yafang 已提交
66 67 68 69 70 71 72 73 74 75 76 77
   }
   // FoodDataModels.ets
   import { Category, FoodData } from './FoodData'
   
   export function initializeOnStartup(): Array<FoodData> {
     let FoodDataArray: Array<FoodData> = []
     FoodComposition.forEach(item => {
       FoodDataArray.push(new FoodData(item.name, item.image, item.category, item.calories, item.protein, item.fat, item.carbohydrates, item.vitaminC ));
     })
     return FoodDataArray;
   }
   ```
Z
zengyawen 已提交
78 79 80


The data resources for the diet application are now ready. You can continue to create a food category list by loading the data.