」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 使用 Oats~i 建立 Web 應用程式 – 課程片段、路由和預設路由

使用 Oats~i 建立 Web 應用程式 – 課程片段、路由和預設路由

發佈於2024-08-23
瀏覽:634

Today, we’re not talking theory or starter projects. We’re building our very own, first ever, Oats~i web app in a tutorial.

If you’re reading this for the first time, briefly check out the start of the “Build a Web with Oats~i” series to learn what Oats~i is, a bit of how it works and how to set it up in your frontend development environment.

In this tutorial, we’re going to build our very own Oats~i web app that will have two pages, one where we’ll show random cat pictures and another where we’ll search and show movies based on titles, all using publicly available APIs.

Build a Web App with Oats~i – Lesson Fragments, Routing, and Default Routes

Build a Web App with Oats~i – Lesson Fragments, Routing, and Default Routes

However, with so much to learn, we’ll be taking things one step at a time. So, in lesson 1, our objective is to set up our routing and fragments and showcase how to set up a default route.

Our final project for today’s lesson will look something like this.

Build a Web App with Oats~i – Lesson Fragments, Routing, and Default Routes

Build a Web App with Oats~i – Lesson Fragments, Routing, and Default Routes

Note:

If you want to run the final code before we get started, or want to follow along directly, find the source code here: https://github.com/Ian-Cornelius/Oats-i-Lesson-1---Fragments-Routes-and-Default-Route

Clone the repo, navigate to the project’s root folder, open the terminal, then run

npm install

Wait for the installation to complete, then run

npm run dev

Open the url given in the terminal in your browser, navigate around, then follow along.

Also, in this whole series, I’ll assume you’re familiar with html and css in general, to ensure we only stick to the bits of code that matter when creating an Oats~i web app. There are no advanced HTML or CSS knowledge needed to get started with Oats~i. Just the basics.

Also, I assume you have node and npm installed already.

Now, let’s dive in!

Setting Up

We’ll quickly set up our project using the Oats~i cli. Create a new folder where you want to run Oats~i, navigate to it, open the terminal, then run

npx oats-i-cli

Follow the prompts and wait for the installation to complete.

You’ve just set up a complete Oats~i development environment. Now let’s get to deleting and editing the files in the starter project that we don’t need.

Deleting Files

First, navigate to src -> app -> index -> assets and delete all files under that folder.

Then, navigate to src -> app -> fragments and delete the about and home folders.

Create the Cats and Movies Fragments

Oats~i renders views or pages primarily through a fragment system. A fragment is basically a view unit, comprising of components or elements necessary to show visual detail or data.

Let’s take the case of our tutorial project.

We’ll have a page where a user can click on a button and have random cat images shown to them based on randomly selected http codes, using the http-cat API. This will be the home page or “/” route.

On the other page, the user will have a search bar and button where they can input a movie title and search for it using the movies API. This is the movies page or “/movies” route.

We want the user to be able to navigate between these two pages, with the right view being shown when in the home page or “/” route and movies page or “/movies” route respectively.

These views are our fragments. Here’s a visualization to help make this concept clearer.

Build a Web App with Oats~i – Lesson Fragments, Routing, and Default Routes

Build a Web App with Oats~i – Lesson Fragments, Routing, and Default Routes

By this logic, our Oats~i web app for this project will have two fragments. Let’s create these two, each having its own folder where its complete code is contained.

The Cats Fragment Folder

Navigate to src -> app -> fragments then create a new folder named “http-cats”. This folder will hold the source code for the http-cats fragment in our Oats~i app, shown when the user navigates to the
“/” route.

Inside the “http-cats” folder, create new folders named views, styles, assets, and scripts.

The views folder will hold the view templates for our http-cats fragment. The styles folder will hold the css files, the assets folder will hold the asset files (images, etc), and the scripts folder will hold our javascript files.

Note: These folder structures are not a strict enforcement by Oats~i but rather a logical project structure that I find to work best for most cases.

Now, let’s write the JavaScript source file that will actually render our http-cats fragment.

Write the Http-Cats Fragment Source File

Inside the scripts folder, create a new file named http_cats_main_fragment.js. Paste the following code inside it:

//@ts-check
//import styles
import "../styles/http_cats.css";

import AppFragmentBuilder from "oats-i/fragments/builder/AppFragmentBuilder";
import AppMainFragment from "oats-i/fragments/AppMainFragment"

class HTTPCatsMainFragment extends AppMainFragment{

    async initializeView(cb){

        //@ts-expect-error cannot find module (for view)
        const uiTemplate = require("../views/http_cats_fragment.hbs")();
        this.onViewInitSuccess(uiTemplate, cb);
    }
}

const httpCatsMainFragmentBuilder = new AppFragmentBuilder(HTTPCatsMainFragment, {

    localRoutingInfos: null,
    viewID: "http-cats-main-fragment",
});
export default httpCatsMainFragmentBuilder;

Now, let’s break it down.

Importing CSS Files (Styling)

The first line imports our css file, used to style our http-cats fragment view. Webpack handles loading this file into our final HTML file using the css loader, style loader, and html webpack plugin we’d configured before.

AppMainFragment, AppFragmentBuilder (and AppChildFragment Classes - Fragment Classes)

The next lines import the AppMainFragment class and the AppFragmentBuilder class.

When building Oats~i fragments, you’ll be extending from either of two fragment classes. These are the main fragment or child fragment class.

The main fragment is the parent fragment view of your page or route. Its content is encapsulated within a div element, given the id passed in as the viewID when instantiating the AppFragmentBuilder class. This content is then placed inside the tag which you put inside the tag.

There can only be one main fragment per route or page. Any other view that will be rendered by a fragment afterwards must be from a child fragment. You can picture it this way using a HTML markup tree.

HTTP Cats
                Movies
            
        
        
版本聲明 本文轉載於:https://dev.to/oatsi/build-a-web-app-with-oatsi-lesson-1-fragments-routing-and-default-routes-2en0?1如有侵犯,請聯絡study_golang @163.com刪除
最新教學 更多>
  • 如何解決 JLabel 拖放的滑鼠事件衝突?
    如何解決 JLabel 拖放的滑鼠事件衝突?
    用於拖放的JLabel 滑鼠事件:解決滑鼠事件衝突為了在JLabel 上啟用拖放功能,滑鼠事件必須被覆蓋。然而,當嘗試使用 mousePressed 事件實作拖放時,會出現一個常見問題,因為 mouseReleased 事件對該 JLabel 無效。 提供的程式碼在 mousePressed 事件中...
    程式設計 發佈於2024-11-06
  • MySQL 中的資料庫分片:綜合指南
    MySQL 中的資料庫分片:綜合指南
    随着数据库变得越来越大、越来越复杂,有效地控制性能和扩展就出现了。数据库分片是用于克服这些障碍的一种方法。称为“分片”的数据库分区将大型数据库划分为更小、更易于管理的段(称为“分片”)。通过将每个分片分布在多个服务器上(每个服务器保存总数据的一小部分),可以提高可扩展性和吞吐量。 在本文中,我们将探...
    程式設計 發佈於2024-11-06
  • 如何將 Python 日期時間物件轉換為秒?
    如何將 Python 日期時間物件轉換為秒?
    在Python 中將日期時間物件轉換為秒在Python 中使用日期時間物件時,通常需要將它們轉換為秒以適應各種情況分析目的。但是,toordinal() 方法可能無法提供所需的輸出,因為它僅區分具有不同日期的日期。 要準確地將日期時間物件轉換為秒,特別是對於 1970 年 1 月 1 日的特定日期,...
    程式設計 發佈於2024-11-06
  • 如何使用 Laravel Eloquent 的 firstOrNew() 方法有效最佳化 CRUD 操作?
    如何使用 Laravel Eloquent 的 firstOrNew() 方法有效最佳化 CRUD 操作?
    使用 Laravel Eloquent 優化 CRUD 操作在 Laravel 中使用資料庫時,插入或更新記錄是很常見的。為了實現這一點,開發人員經常求助於條件語句,在決定執行插入或更新之前檢查記錄是否存在。 firstOrNew() 方法幸運的是, Eloquent 透過firstOrNew() ...
    程式設計 發佈於2024-11-06
  • 為什麼在 PHP 中重寫方法參數違反了嚴格的標準?
    為什麼在 PHP 中重寫方法參數違反了嚴格的標準?
    在PHP 中重寫方法參數:違反嚴格標準在物件導向程式設計中,里氏替換原則(LSP) 規定:子類型的物件可以替換其父對象,而不改變程式的行為。然而,在 PHP 中,用不同的參數簽名覆蓋方法被認為是違反嚴格標準的。 為什麼這是違規? PHP 是弱型別語言,這表示編譯器無法在編譯時確定變數的確切型別。這表...
    程式設計 發佈於2024-11-06
  • 哪個 PHP 函式庫提供卓越的 SQL 注入防護:PDO 還是 mysql_real_escape_string?
    哪個 PHP 函式庫提供卓越的 SQL 注入防護:PDO 還是 mysql_real_escape_string?
    PDO vs. mysql_real_escape_string:綜合指南查詢轉義對於防止 SQL 注入至關重要。雖然 mysql_real_escape_string 提供了轉義查詢的基本方法,但 PDO 成為了一種具有眾多優點的卓越解決方案。 什麼是 PDO? PHP 資料物件 (PDO) 是一...
    程式設計 發佈於2024-11-06
  • React 入門:初學者的路線圖
    React 入門:初學者的路線圖
    大家好! ? 我剛開始學習 React.js 的旅程。這是一次令人興奮(有時甚至具有挑戰性!)的冒險,我想分享一下幫助我開始的步驟,以防您也開始研究 React。這是我的處理方法: 1.掌握 JavaScript 基礎 在開始使用 React 之前,我確保溫習一下我的 JavaScript 技能,...
    程式設計 發佈於2024-11-06
  • 如何引用 JavaScript 物件中的內部值?
    如何引用 JavaScript 物件中的內部值?
    如何在JavaScript 物件中引用內部值在JavaScript 中,存取引用同一物件中其他值的物件中的值有時可能具有挑戰性。考慮以下程式碼片段:var obj = { key1: "it ", key2: key1 " works!" }; a...
    程式設計 發佈於2024-11-06
  • Python 列表方法快速指南及範例
    Python 列表方法快速指南及範例
    介紹 Python 清單用途廣泛,並附帶各種內建方法,有助於有效地操作和處理資料。以下是所有主要清單方法的快速參考以及簡短的範例。 1. 追加(項目) 將項目新增至清單末端。 lst = [1, 2, 3] lst.append(4) # [1, 2, 3, ...
    程式設計 發佈於2024-11-06
  • C++ 中何時需要使用者定義的複製建構函式?
    C++ 中何時需要使用者定義的複製建構函式?
    何時需要使用者定義的複製建構子? 複製建構子是 C 物件導向程式設計的組成部分,提供了一種基於現有實例初始化物件的方法。雖然編譯器通常會為類別產生預設的複製建構函數,但在某些情況下需要進行自訂。 需要使用者定義複製建構子的情況當預設複製建構子不夠時,程式設計師會選擇使用者定義的複製建構子來實作自訂複...
    程式設計 發佈於2024-11-06
  • 試...捕捉 V/s 安全分配 (?=):現代發展的福音還是詛咒?
    試...捕捉 V/s 安全分配 (?=):現代發展的福音還是詛咒?
    最近,我發現了 JavaScript 中引入的新安全賦值運算子 (?.=),我對它的簡單性著迷。 ? 安全賦值運算子 (SAO) 是傳統 try...catch 區塊的簡寫替代方案。它允許您內聯捕獲錯誤,而無需為每個操作編寫明確的錯誤處理程式碼。這是一個例子: const [error, resp...
    程式設計 發佈於2024-11-06
  • 如何在Python中優化固定寬度檔案解析?
    如何在Python中優化固定寬度檔案解析?
    優化固定寬度文件解析為了有效地解析固定寬度文件,可以考慮利用Python的struct模組。此方法利用 C 來提高速度,如下例所示:import struct fieldwidths = (2, -10, 24) fmtstring = ' '.join('{}{}'.format(abs(fw),...
    程式設計 發佈於2024-11-06
  • 蠅量級
    蠅量級
    結構模式之一旨在透過與相似物件共享盡可能多的資料來減少記憶體使用。 在處理大量相似物件時特別有用,為每個物件建立一個新實例在記憶體消耗方面會非常昂貴。 關鍵概念: 內在狀態:多個物件之間共享的狀態獨立於上下文,並且在不同物件之間保持相同。 外部狀態:每個物件唯一的、從客戶端傳遞的狀態。此狀態可...
    程式設計 發佈於2024-11-06
  • 解鎖您的 MySQL 掌握:MySQL 實作實驗室課程
    解鎖您的 MySQL 掌握:MySQL 實作實驗室課程
    透過全面的 MySQL 實作實驗室課程提升您的 MySQL 技能並成為資料庫專家。這種實踐學習體驗旨在引導您完成一系列實踐練習,使您能夠克服複雜的 SQL 挑戰並優化資料庫效能。 深入了解 MySQL 無論您是想要建立強大 MySQL 基礎的初學者,還是想要提升專業知識的經驗豐富的...
    程式設計 發佈於2024-11-06

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3