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

使用 Oats~i 建立 Web 應用程式 – 啟動 Oats~i 應用程式

發佈於2024-08-27
瀏覽:684

Welcome to the second part of the Build a Web App with Oats~i tutorial series. In the first part, we went through installing Oats~i in your development environment. In case you missed that one, check it out here.

In this part of the series, we’ll go through how to start an Oats~i app. This will cover all instances where you want to start an Oats~i app and have the framework run on the front end.

Instead of building a whole project from scratch, we’ll use the inbuilt starter project that comes with Oats~i when you set it up using the Oats~i cli. We’ll open the important bits of the code necessary for this tutorial and use that to explain how Oats~i loads up when it gets to the client/browser and starts running your web app.

Let’s dive in.

Install the Starter Project Using Oats~i CLI

Create a new folder where you want to install the Oats~i starter project and open the terminal. Then run:

npx oats-i-cli

Follow the prompts.

This will install Oats~i and the starter project bundled with it to your current working directory.

Finally, to run the starter project, run

npm run dev

Navigate to the address provided in the terminal (often localhost:8080) and see the starter project in action. Navigate back and forth between the available pages to see Oats~i handle routing and build views through fragments.

Now let’s get to the code that brings Oats~i to life in the client/browser.

Index.js

Open the index.js file found in src -> app -> index -> scripts.

Build a Web App with Oats~i – Starting an Oats~i App

This file contains the code that starts Oats~i for the starter project bundled in the cli. This is more or less the same code you’d write to start Oats~i in your own project.

Let’s break it down.

The App Root

Oats~i is typically initialized from the appRoot module through the appRoot.initApp() method.

appRoot is a singleton instance of the AppRoot class that manages the initialization of an Oats~i web app, main navigation, and loading the root view if its template is provided.

The initApp() method takes several arguments, including an instance of the AppStateManager, MainRouter, AppRootView object, a default route, and optional extra options that currently allow you to define the app’s external link interception behavior.

Let’s break these down.

AppStateManager

The app state manager is a module responsible for managing the route states of an Oats~i app. Primarily, it helps Oats~i understand whether a history pop event is a click on the forward or back button on the browser by the user.

This information is not obvious using the history API as is.

Having this information is crucial because Oats~i saves fragment states as the web app is navigated by the user. Therefore, if they are to go back or forward to a page they were in before, the fragment(s) responsible for rendering and running those pages will “remember” their state and show the right information and automatically be scrolled to the right position the user was at.

Knowing whether the pop event is a forward or back movement makes retrieving state information accurate.

You simply create an instance of the AppStateManager by invoking new, passing in the web app’s routing info.

In our index.js file, we do this in the line:

const appStateManager = new AppStateManager(AppRoutingInfo);

We have already defined our AppRoutingInfo in a separate file (more on this later).

Main Router

The main router handles routing in an Oats~i web app. You create a router instance by invoking “new”, passing in the app’s routing info, app state manager instance, an error callback, a root path, and a route consent callback.

We’ve already talked about the app routing info and app state manager instance, so let’s talk about the other arguments that you pass to the main router.

Error Callback

In case of a problem routing, the main router can fire the error callback and inform you of the reasons why routing has failed. As long as you’ve defined your routes well, you shouldn’t worry about implementing this method, other than merely providing it.

Root Path

The main router allows you to directly call for routing from your JavaScript code using the routeTO() method. It also determines the valid route to follow based on your app’s main routing info.

Now, if you have Oats~i running in your website with a special address such as /admin, it can be repetitive having to start every routing call or routing info with the /admin root. Instead, you can supply this to the main router as the root path, and just append the rest of your route to your routing calls or routing info.

So, instead of /admin/create, with the root path set to /admin, you can just have the route as /create. However, href attributes in links MUST be fully qualified.

Route Consent Callback

Before the main router can trigger routing for a given path, it attempts to see whether routing for that route has been permitted. The route consent callback allows you to specify this, giving you the ability to control which sections of your web app can be accessed by a given user or based on any other business or app logic.

Note: This is different from fragment consenting, which we’ll cover later.

AppRootView Object

The app root view object consists of the root view template and array of main navigation infos.

Every Oats~i app has a root view. This is the permanent section of the view which the user will always see regardless of the path they’re navigating to. The root view is wrapped within an tag and will typically contain elements such as your navbar and footer.

Here’s an example, sourced from home.sv.hbs in the Oats~i starter project

Home
            About
        
        

Note: As you can infer from the Oats~i starter project, you can skip passing in a template for the app root view in the initApp() method as long as you’ve provided it in the html markup sourced for your page from the server.

Your root view object should also have a single tag specified within, where the app’s fragments will be loaded.

The AppRootView object also takes a main navigation info array, which contains a list of selectors and their active routes, which Oats~i will use to show you which navigation selector is active.

In the Oats~i starter project, specifying the main nav info makes it possible to update the styling and looks of the navigation links when you click on it and navigate to its url.

Oats~i handles the click and update boiler plate for the navigation links or buttons. All you have to do is style the links or buttons based on which one is marked as active using the attribute ‘navigation-state=”active”’

Default Route

The default route is the route Oats~i will default to incase the page loads from a url that is not specified in your app’s routing info. You can use this to curate the default web app behavior especially if it results from a log in or authentication action.

For instance, if the user has come in from a login page and is redirected to their admin page, but the initial page load is at “/admin”, you can specify the default route to be “/admin/dashboard”.

Oats~i will reroute the web app to /admin/dashboard on load and the user will always access their dashboard every time they come from logging into the app.

If you want to change this behavior to say, profile, you can simply change the default route in your web app to /admin/profile and all users logging in will have their profile page as the default page.

Extra Options

Oats~i also takes in optional extra options, with the current implementation allowing you to intercept external links within your web app. This allows you to warn users of their navigation away from your site and the url they’re about to access, in case you want to build a content protection system.

Complete Code

The complete code for starting an Oats~i app will look something like this:

//sourced from index.js in the starter app
const appStateManager = new AppStateManager(AppRoutingInfo);
    appRoot.initApp(appStateManager, new MainRouter(AppRoutingInfo, appStateManager, (args) => {}, "", async (url) => {

        return {

            canAccess: true,
            fallbackRoute: "/"
        }
    }), { template: null, mainNavInfos: AppMainNavInfo }, "");

You can wrap this code within a function that is automatically invoked when your index.js file downloads in the browser to have the web app automatically started on page load. In the starter project’s case, this is the initApp() function.

Now, there are two more things left to explain.

App Routing Info

From the startup code, you can point out that providing routing information is mandatory for an Oats~i web app to initialize. From the example’s I’ve given, we define this info elsewhere then import it in our startup script. Defining your routing info in a separate file is not mandatory, but I find it to be good practice especially when you want to add more routes or main navigation in your web app.

Here’s how routing information is defined for an Oats~i web app, using the example from the starter project.

const AppRoutingInfo = RoutingInfoUtils.buildMainRoutingInfo([

    {
        route: "/",
        target: homeMainFragmentBuilder,
        nestedChildFragments: null
    },
    {
        route: "/about",
        target: aboutMainFragmentBuilder,
        nestedChildFragments: null
    }
], AppMainNavInfo);

You can call the routing info any name you want, as long as you use the method RoutingInfoUtils.buildMainRoutingInfo() to build it.

The method will return an array of routing information, with each info holding:

  • route: this is the route that info is valid for. You cannot repeat a route across your routing infos.
  • target: this is the main fragment the route should start building from. Oats~i builds its fragments from the main fragment to the child fragments
  • Nested child fragments: these are the child fragments that should also be built for the route, in order. This order typically represents the DOM structure, with the fragment that should be created first in DOM coming before it’s nested child or a child fragment in the same level with it.

You can notice that we also pass AppMainNavInfo to the buildMainRoutingInfo() method as well. This is the same info we passed in to the app state manager and initApp method in the starter script. Let’s look at it.

App Main Nav Info

A good web app needs good navigation. And often, setting up navigation can involve a lot of boiler plate code to not only set up the click listeners for the navigation links or buttons, but also change their styling based on which one is active.

Oats~i takes care of the boiler plate for you by requiring you only provide the navigation information needed by your web app. This takes the following format (example sourced from the starter project).

const AppMainNavInfo = MainNavigationInfoBuilder.buildMainNavigationInfo([

    {
        selector: "home-link",
        defaultRoute: "/",
        baseActiveRoute: "/",
    },
    {
        selector: "about-link",
        defaultRoute: "/about",
        baseActiveRoute: "/about",
    }
]);

You create a main navigation info in Oats~i using the method MainNavigationInfoBuilder.buildMainNavigationInfo(). It returns an array of navigation infos, with each containing:

  • selector: This is a dot selector that Oats~i will use to query the navigation link or button, set up listeners (for buttons only – links/A tags are automatically intercepted), and update its “navigation-state” attribute based on whether its active or not. In your markup, this is simply a class name.
  • defaultRoute: this is the default route Oats~i should route to if the navigation button is clicked. For links, since Oats~i automatically intercepts clicks on links/A tags, ensure this matches the value of your href attribute.
  • baseActiveRoute: This is the base route for which the navigation button or link should be treated as active. For instance, if you have a navigation button with defaultRoute “/profile” and baseActiveRoute “/profile” and the user navigates to /profile/update-pic, Oats~i will set this button as active since this route starts with the baseActiveRoute value.

Putting Everything Together

Let’s put everything together and have the final picture of what you need to get an Oats~i app up and running. Typically, you must have defined:

  • The app routing info
  • The app main nav info (for main navigation links in your root view)

Then, simply get a new instance of the app state manager, and call appRoot.initApp passing this instance, the app routing info, app main nav info, and main router instance. Again, here’s the example code from the Oats~i starter project that comes bundled with the cli, now wrapped in a function that we call immediately the index.js file loads in the browser.

function initApp(){

    const appStateManager = new AppStateManager(AppRoutingInfo);
    appRoot.initApp(appStateManager, new MainRouter(AppRoutingInfo, appStateManager, (args) => {}, "", async (url) => {

        return {

            canAccess: true,
            fallbackRoute: "/"
        }
    }), { template: null, mainNavInfos: AppMainNavInfo }, "");
}

initApp();

As your web app grows and changes, the only bits of this code you’ll ever need to change are the app routing infos and main navigation infos (AppRoutingInfo and AppMainNavInfo). This will be typically because you’re adding new routes to your web app (thus updating the routing information) or adding new main navigation buttons or links (thus updating the main navigation infos).

The rest of the code will be fine untouched.

Sign Up and Follow for the Next Tutorial

A lot of this will make sense when we build our very own small project in Oats~i. That’s what we’ll be doing in the next part of this series, to learn the next fundamental concepts around building an Oats~i web app.

See you then.

Support Oats~i

You can support the development of Oats~i through Patreon or buy me a coffee.

版本聲明 本文轉載於:https://dev.to/oatsi/build-a-web-app-with-oatsi-starting-an-oatsi-app-2pd?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • Go 中的並發模式;工作池和扇出/扇入
    Go 中的並發模式;工作池和扇出/扇入
    Go 以其卓越的並發模型而聞名,但許多開發人員只專注於 goroutine 和通道。然而,工作池和扇出/扇入等並發模式提供了真正的效率。 本文將介紹這些進階概念,幫助您最大限度地提高 Go 應用程式的吞吐量。 為什麼並發很重要 並發允許程式有效率地執行任務,特別是在處理 I/O ...
    程式設計 發佈於2024-11-06
  • 如何在 C++ 中將單一字元轉換為 std::string?
    如何在 C++ 中將單一字元轉換為 std::string?
    從單字建立字串從單一字元建立字串人們可能會遇到需要將表示為char 資料類型的單字轉換為std:: string。從字串中取得字元很簡單,只需在所需位置索引字串即可。然而,相反的過程需要不同的方法。 要從單字建立std::string,可以使用多種方法:char c = 34; std::strin...
    程式設計 發佈於2024-11-06
  • JavaScript 變數名稱中美元符號的含義是什麼?
    JavaScript 變數名稱中美元符號的含義是什麼?
    JavaScript 變數名稱中美元符號的意義在程式設計領域,命名約定的使用對於增強程式碼至關重要可讀性並遵循最佳實務。在 JavaScript 中,美元符號 ($) 通常會作為變數名稱的前綴出現,特別是引用 jQuery 物件的變數名稱。 美元符號的用途是什麼? 與流行的看法相反,JavaScri...
    程式設計 發佈於2024-11-06
  • 如何重新排列 CSS 網格佈局中的列以實現移動響應?
    如何重新排列 CSS 網格佈局中的列以實現移動響應?
    在CSS 網格佈局中重新排序列在CSS 網格佈局中,有多種技術可以修改列的順序以實現具體佈局。本問題探討了重新排列行動佈局列的可能性,例如將列移到底部,同時在桌面佈局上保持所需的列順序。 解決方案選項:grid-template-areas: 此屬性可讓您在網格內定義命名區域,然後將網格項目指派給這...
    程式設計 發佈於2024-11-06
  • Hacktoberfest 週線上拍賣系統
    Hacktoberfest 週線上拍賣系統
    概述 在 Hacktoberfest 的第三週,我決定為一個較小但有前途的專案做出貢獻:線上拍賣系統。儘管該專案仍處於早期階段,但它已經顯示出成長潛力,而且我看到了幫助改進其程式碼庫的機會。我的任務是透過減少冗餘程式碼和改進整體結構來重構項目,使其更具可維護性和可擴展性。 ...
    程式設計 發佈於2024-11-06
  • 如何使用“exception_ptr”在 C++ 執行緒之間傳播異常?
    如何使用“exception_ptr”在 C++ 執行緒之間傳播異常?
    在C 中的線程之間傳播異常當從主線程調用的函數生成多個線程時,就會出現在C 中的執行緒之間傳播異常的任務用於CPU 密集型工作的工作執行緒。挑戰在於處理工作執行緒上可能發生的異常並將其傳播回主執行緒以進行正確處理。 傳統方法一種常見方法是手動捕獲工作線程上的各種異常,記錄它們的詳細信息,然後在主線程...
    程式設計 發佈於2024-11-06
  • 如何使用 3D CSS 轉換來修復 Firefox 中的鋸齒狀邊緣?
    如何使用 3D CSS 轉換來修復 Firefox 中的鋸齒狀邊緣?
    使用3D CSS 變換時Firefox 中的鋸齒狀邊緣與Chrome 中使用CSS 變換時的鋸齒狀邊緣問題類似,Firefox 在3D 變換中也出現了這個問題。背面可見性作為 Chrome 中的潛在解決方案,在 Firefox 中被證明無效。 解決方案:要在Firefox 中緩解此問題,您可以實施以...
    程式設計 發佈於2024-11-06
  • 為什麼 PHP 的 mail() 函數會為電子郵件發送帶來挑戰?
    為什麼 PHP 的 mail() 函數會為電子郵件發送帶來挑戰?
    為什麼PHP 的mail() 函數達不到要求:限制和陷阱雖然PHP 提供了mail() 函數用於發送電子郵件,但它卻失敗了與專用庫或擴展相比較短。以下是與使用mail() 相關的缺點和限制的全面檢查:格式問題:mail() 可能會遇到以下問題:標題和內容格式,尤其是作業系統之間的換行差異。這些錯誤可...
    程式設計 發佈於2024-11-06
  • 使用 npyConverter 簡化 NumPy 檔案轉換
    使用 npyConverter 簡化 NumPy 檔案轉換
    如果您使用 NumPy 的 .npy 檔案並需要將其轉換為 .mat (MATLAB) 或 .csv 格式,npyConverter 就是適合您的工具!這個簡單的基於 GUI 的工具透過乾淨且用戶友好的介面提供 .npy 檔案的批量轉換。 主要特點 批次轉換:將目錄下所有.npy檔...
    程式設計 發佈於2024-11-06
  • 如何停用特定線路的 Eslint 規則?
    如何停用特定線路的 Eslint 規則?
    停用特定行的Eslint 規則在JSHint 中,可以使用語法停用特定行的linting 規則: /* jshint ignore:start */ $scope.someVar = ConstructorFunction(); /* jshint ignore:end */對於 eslint,有幾...
    程式設計 發佈於2024-11-06
  • 如何在沒有錯誤的情況下將清單插入 Pandas DataFrame 單元格?
    如何在沒有錯誤的情況下將清單插入 Pandas DataFrame 單元格?
    將清單插入Pandas 儲存格問題在Python 中,嘗試將清單插入Pandas DataFrame 的儲存格可能會導致錯誤或意圖想不到的結果。例如,當嘗試將清單插入DataFrame df 的儲存格1B 時:df = pd.DataFrame({'A': [12, 23], 'B': [np.na...
    程式設計 發佈於2024-11-06
  • Matplotlib 中的「plt.plot」、「ax.plot」和「figure.add_subplot」之間的主要差異是什麼?
    Matplotlib 中的「plt.plot」、「ax.plot」和「figure.add_subplot」之間的主要差異是什麼?
    Matplotlib 中繪圖、軸與圖形之間的差異Matplotlib 是一個用於建立視覺化的物件導向的 Python 函式庫。它使用三個主要物件:圖形、軸和繪圖。 圖形圖形表示將在其中顯示可視化的整個畫布或視窗。它定義畫布的整體大小和佈局,包括邊距、背景顏色和任何其他全域屬性。 軸軸表示圖中繪製資料...
    程式設計 發佈於2024-11-06
  • FireDucks:以零學習成本獲得超越 pandas 的效能!
    FireDucks:以零學習成本獲得超越 pandas 的效能!
    Pandas 是最受歡迎的庫之一,當我在尋找一種更簡單的方法來加速其性能時,我發現了 FireDucks 並對它產生了興趣! 與 pandas 的比較:為什麼選擇 FireDucks? Pandas 程式可能會遇到嚴重的效能問題,這取決於其編寫方式。然而,作為一名數據科學家,我想花...
    程式設計 發佈於2024-11-06
  • CSS 網格:嵌套網格佈局
    CSS 網格:嵌套網格佈局
    介紹 CSS Grid 是一種佈局系統,因其在創建多列佈局方面的靈活性和效率而迅速受到 Web 開發人員的歡迎。它最有用的功能之一是能夠建立嵌套網格佈局。嵌套網格可以在設計複雜網頁時提供更多控制和精確度。在本文中,我們將探討在 CSS Grid 中使用嵌套網格佈局的優點、缺點和主要...
    程式設計 發佈於2024-11-06
  • 適用於 Java 的 Jupyter 筆記本
    適用於 Java 的 Jupyter 筆記本
    Jupyter Notebook 的强大 Jupyter Notebooks 是一个出色的工具,最初是为了帮助数据科学家和工程师使用 python 编程语言简化数据处理工作而开发的。事实上,笔记本的交互性使其非常适合快速查看代码结果,而无需搭建开发环境、编译、打包等。此功能对于数据...
    程式設計 發佈於2024-11-06

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

Copyright© 2022 湘ICP备2022001581号-3