」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 綁定和模板:Peasy-UI 系列的一部分

綁定和模板:Peasy-UI 系列的一部分

發佈於2024-11-08
瀏覽:544

Bindings and Templates: Part f the Peasy-UI series

Table of Contents

  1. Introduction
  2. Bindings and the Template
  3. Text Bindings
    • Basic Binding
    • Conditional Boolean Text Binding
    • One-time Text bindings
  4. Attribute Bindings
    • Bindings for element attributes
    • Bindings for events
    • Binding whole elements to data model
    • Binding for Rendering
    • Binding for Arrays
    • Bindings for Components
  5. Getters
  6. More information
  7. Conclusion

Introduction

Today we are going to dive a layer deeper into the Peasy-UI library. We are going to explain the different types of bindings that are available in the library, and how you can incorporate them into the string literal template that you provide to the library to be rendered to the DOM. For this article all examples are provided using TypeScript, but the library works with vanilla JavaScript perfectly fine.

If you missed the introduction article for Peasy-UI, you can read it
here, and it can provide the overview for the library, and even introduces upcoming topics in the series.

Bindings and the Template

I quick recap of what bindings are, what the template is, and how they work with the library to give you control of what gets rendered to the DOM.

Peasy-UI is a library that injects content into DOM elements that you specify. The content that gets injected is dictated by a string literal template that provides HTML markup that represents the content to be injected.

const template = `
  
Render Me
`;

This is an example of the string literal template that you pass to Peasy-UI and it will render it into the targeted DOM element.

Bindings provide a means to control that template dynamically. For example, let's do a Hello World example.

const model = {
  name: "Bob",
};

const template = `
Hello: \${name}!!!!
`; await UI.create(document.body, model, template).attached;

This code creates the data store (model), that is bound to the html in the string literal template. UI.create() takes the model, template, and the targeted DOM element to inject into as parameters. In your JavaScript code, you can change model.name to whatever string you'd like, and it will dynamically re-render the new text into the DOM. Side note: the await in the UI.create() method pauses the execution of subsequent logic until Peasy-UI attaches the content into the DOM. This way you can prevent trying to make changes or access DOM content that isn't rendered yet.

Text bindings

There are two types of bindings in Peasy-UI, text bindings and attribute bindings, and we will explore both types. One thing to note is that since we are providing a string literal to Peasy-UI, we use the backslash escape character to designate it as a binding.

Basic binding

The most basic binding is simply injecting data into the DOM as text. The Hello World above is a prime example of this. Use cases for this binding can include manipulating the inner text or inner HTML content inside a DOM element, such as changing a name. I've also used this for changing CSS properties inside a CSS string.

BINDING: ${propertyName}

As in the example above, we can use this:

const model = {
  name: "Bob",
};

const template = `
Hello: \${name}!!!!
`;

The binding maps the 'name' property to the tag in the HTML and replaces name with the name content from the model.

Conditional boolean text binding

In order to control if a binding is active, we can leverage some conditional bindings.

BINDING: ${'value' = propertyName}

BINDING: ${'value' ! propertyName}

An example of this would be:

const model = {
  darkMode: true,
};

const template = `
I am using \${'Dark mode' = darkMode} \${'Light mode' ! darkMode} now!
`;

Based on the truthy status of the darkMode property in the model, different text renders. If true, the div will say I am using Dark mode now! and if its set to false, it will render I am using Light mode now!.

One-time text bindings

The previous bindings get constantly evaluated with respect to RequestAnimationFrame(). However, sometimes you may only want to evaluate it one-time, like on initialization. Peasy-UI provides one-time bindings.

BINDING: ${|'value' = prop}

BINDING: ${|'value' ! prop}

The same example above can be reused, the only difference is that the evaluation of the darkMode property will only be done on the first rendering cycle. Please take note of the pipe character in the binding, as that is what designates it as a one-time binding.

Attribute Bindings

Attribute bindings use either \${ } or the pui attribute within an element tag to affect the behaviour of the element. For the sake of this article we will continue using the \${ } nomenclature. We will explore the different methods of assigning bindings in a future article.

Bindings for element attributes

Peasy-UI gives us the power to access element attributes and allows us to monitor and control those values easily.

There are four basic element attribute bindings:

BINDING: ${attribute

BINDING: ${attribute

BINDING: ${attribute ==> propertyName} bind in one direction, from the element to the model

BINDING: ${attribute propertyName} bi-directional, or two-way, between the element and the model

Let's look at some examples of this:

const model = {
  color: "red",
};

const template = `
color}>Red color}>Green
`;

In this example you are binding a custom attribute on each element 'red' and 'green' to the color property in the model. When the radio button in this group is selected, it changes the data in the model property 'color'.

Another simple example:

const model = {
  userName: "",
};

const template = `
userName}>
`;

This is a one way binding from the text field, that sets the data model value of userName to whatever is entered into the input field.

Let's look at a bidirectional example:

const model = {
  userName: "Bob",
};

const template = `
userName}>\${userName}
`;

The magic that exists here is that you can set username in the data model programmatically, and it will automatically change the rendered text in the input field. Or, you can type into the input field and it will dynamically change the value in the data model.

Bindings for events

So what if want to render a drop down select input, or a button? Peasy-UI gives you access to mapping the event listeners automatically and tying them into the data model.

BINDING: ${ event @=> callbackName}

Buttons provide easy examples for this, but all DOM events can be used.

const model = {
  clickHandler: (event: HTMLEvent, model: any, element: HTMLElement, eventType: string, object: any) => {
    window.alert("I got clicked");
  },
};

const template = `
`;

Okay, there is a bit to unpack here. First lets focus on the binding, you can bind any HTML DOM event, such as change, input, click, mouseenter, etc... as an event binding, and then you provide the 'handler' callback which exists IN the data model.

Peasy passes 5 standard parameters into the handler for your convenience. The first parameter is the HTMLEvent that actually is fired.The second parameter is the 'localized' data model that is bound to the element. This CAN be tricky, because depending on your nesting of elements, it maybe be a localized data model for just that element, or if it is a top level binding, it will be the standard data model. We will get into nesting a bit later, but just know that another option is provided to help with this.

The third parameter is the target element that fired the event, so you can access any attributes nested in the element. The fourth element is the string representation of the event, such as 'click' or 'change' and this helps in case you bind multiple events to the same callback, you can distinguish which event triggered this callback. The final parameter is the overarching data model. Inside this object, regardless of how nested your binding is, will be an object that contains the model property, and you can navigate that object to have access to ANY property in the data model.

Binding whole elements to data model

Do you know how annoying it is to have to perform a documement.getElementById('myElement'); in your Javascript? Yeah, Peasy-UI makes that a bit easier.

BINDING: ${ ==> propertyName}

Let's look at this timesaver example:

const model = {
  myInputElement: undefined as HTMLElement | undefined,
};

const template = `
myInputElement} />
`;

What this binding does is maps the DOM elements rendered and binds their reference inside the model. In this example, instead of having to do the getElementById() method, model.myInputElement is that reference after the first rendering cycle. The element has to render 'first' then the reference to it gets captured in the data model. This is why its set to undefined by default, then gets set to the element reference after the HTML gets injected into the DOM. So now to have access to the element value, i can just access model.myInputElement.value or any attribute in that element that I'd like access to.

Binding for rendering

What if you want to be selective about what gets rendered and maybe have portions of your template not rendered under certain conditions. Yup, Peasy-UI allows you to control overall rendering as well.

BINDING: ${=== propertyName}

BINDING: ${!== propertyName}

Let's looke at the example of this binding:

const model = {
  isDog: true,
};

const template = `
I am a Dog
I am a Cat

In this example you will only see one of the bound div's being rendered, and by toggling between true and false in the data model.isDog, you can control which div is being rendered.

Binding for arrays

Sometimes you need to list things out in the DOM. Examples may be forum posts, options in a select input, or list items in a nav bar. Peasy-UI gives you an iterable renderer, so you can translate a list or array in the data model to multiple element renderings.

BINDING: ${ item

Let's give a simple example:

const model = {
  myItems: [{text: 'one', value: '1'},{text: 'one', value: '1'},{text: 'one', value: '1'}],
};

const template = `

So this use case iterates over the 'myItems' property array in the data model, and renders a separate option element in the select tag.

Each rendered option tag has its own model scope as you can see, with opt.text and opt.value.

Bindings for components

So with larger projects, there is a desire to organize a code base and utilize code splitting to keep aspects of a project manageable. Peasy-UI allows for component based design. We will 'lightly' touch on this topic in this article as the overall component feature will get its own article for properly exploring its capabilities.

A component in Peasy-UI is any JavaScript object which contains a static, public template property and a static create method.

BINDING: ${ComponentName === state}

Let's look at one basic example, with more to follow in future article:

class MyComponent{
  public static template = `
  
Hello \${name}
My count is: \${count}
`; constructor(public name: string,public count:number){} static create( state: {name:string, count:number}){ return new MyComponent(state.name, state.count) } } const model = { MyComponent, componentData:{ name: 'bob', count: 10, } }; const template = `

So the MyComponent class must have a static create method that accepts incoming state, and a public static template. What Peasy-UI will do is set the local class properties as the internal state for each component, and will inject the class template into the overall project template.

This can let you break a larger project down into smaller pieces. There is a lot more you can do with components, and it warrants its own article to explore further.

Getters

The final binding idea to be covered is the usage of getters in the data model and what they bring to the table. Sometimes a simple primitive just doesn't cut it in the data model, and getters provide a means of providing more complicated logic and the return value will be the binding value to the binding. There's no unique binding tag to cover here, just an example:

const model = {
  val1: 1,
  val2: 3,
  get myGetter(){
      return this.val1   this.val2;
  },
};

const template = `
My getter value is: \${myGetter}

The nice part of using a getter is that it gets re-evaluated every rendering loop. So if any values change within its own logic, it will get updated.

More information

More information can be found in the GitHub repo for Peasy-Lib, you also will find all the other companion packages there too. Also, Peasy has a Discord server where we hang out and discuss Peasy and help each other out.

The author's twitter: Here

The author's itch: Here

Github Repo: Here

Discord Server: Here

Conclusion

So this article covered all the basics of the Peasy-UI library's data bindings, which is the magic that powers how the library is used to render essentially anything you need in the DOM. We discussed Text Bindings and Attribute Bindings. For each we covered the binding syntax, and how it can be used in examples.

In the next article we will dive deeper into events.

版本聲明 本文轉載於:https://dev.to/jyoung4242/bindings-and-templates-part-2-of-the-peasy-ui-series-k44?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 插入資料時如何修復「常規錯誤:2006 MySQL 伺服器已消失」?
    插入資料時如何修復「常規錯誤:2006 MySQL 伺服器已消失」?
    插入記錄時如何解決「一般錯誤:2006 MySQL 伺服器已消失」介紹:將資料插入MySQL 資料庫有時會導致錯誤「一般錯誤:2006 MySQL 伺服器已消失」。當與伺服器的連線遺失時會出現此錯誤,通常是由於 MySQL 配置中的兩個變數之一所致。 解決方案:解決此錯誤的關鍵是調整wait_tim...
    程式設計 發佈於2024-12-22
  • 如何在 Spring 中串流大型 MySQL 結果集以避免 OutOfMemoryError?
    如何在 Spring 中串流大型 MySQL 結果集以避免 OutOfMemoryError?
    使用MySQL 串流處理大型結果集在Spring 應用程式中處理大量MySQL 表時,當驅動程式試圖執行下列操作時,可能會出現OutOfMemoryException:將整個表載入到記憶體中。設定statement.setFetchSize(Integer.MIN_VALUE);可能還不夠,因為這僅...
    程式設計 發佈於2024-12-22
  • 在 SQL 中更新同一個表後,如何安全地更新觸發器內的表?
    在 SQL 中更新同一個表後,如何安全地更新觸發器內的表?
    更新同一個表後更新觸發器中的表在SQL 中,更新觸發器後更新表同一張表提出了一個潛在的問題。這是因為該表已針對更新操作鎖定,並且嘗試在作為相同交易的一部分執行的觸發器內存取該表可能會導致衝突。 要規避此限制,您可以更新透過使用 BEFORE 選項而不是 AFTER 來影響觸發器中的列。這允許您在原始...
    程式設計 發佈於2024-12-22
  • 使用PHP的DOMDocument擷取節點時如何保留HTML標籤?
    使用PHP的DOMDocument擷取節點時如何保留HTML標籤?
    使用DOMDocument 提取HTML 節點的問題簡介DOMDocument 是PHP 類,提供了一種便捷的解析方法以及操作HTML 文件。然而,當嘗試在提取內容時保留 HTML 標籤時,使用者可能會遇到困難。本文深入探討了 DOM 的基本概念,並提出了應對這項挑戰的解決方案。 理解 DOM 和節...
    程式設計 發佈於2024-12-22
  • 如何在 Python 類型提示中指定函數類型?
    如何在 Python 類型提示中指定函數類型?
    在類型提示中指定函數類型在Python中,類型提示用於提供有關變數和函數參數的預期類型的可選元資料。然而,將變數的類型提示指定為函數類型似乎不清楚。 解決方案儘管相關中缺少“typing.Function” PEP 483,您可以使用下列指令將變數的型別提示指定為函式型別"typing.C...
    程式設計 發佈於2024-12-22
  • 為什麼我在發送 URL 編碼的 POST 請求時收到 400 BAD REQUEST?
    為什麼我在發送 URL 編碼的 POST 請求時收到 400 BAD REQUEST?
    帶有URL 編碼負載的POST 請求當構造帶有URL 編碼資料的POST 請求時,經常會遇到400 BAD REQUEST 回應,表示伺服器無法理解負載。當有效負載提供不正確時,通常會出現此問題。 標準函式庫的 http.NewRequest(...) 方法期望將有效負載作為第三個參數提供,該參數應...
    程式設計 發佈於2024-12-22
  • HTML 格式標籤
    HTML 格式標籤
    HTML 格式化元素 **HTML Formatting is a process of formatting text for better look and feel. HTML provides us ability to format text without us...
    程式設計 發佈於2024-12-22
  • 為什麼我不能直接從定義類別中呼叫內聯友元函數?
    為什麼我不能直接從定義類別中呼叫內聯友元函數?
    內聯友元函數的可見性範圍儘管在類別中定義了內聯友元函數,但它們的實際範圍超出了類別的邊界。 C 標準規定,內聯友元函數的名稱引用在最近的封閉命名空間範圍中聲明的函數。 但是,在該命名空間內提供匹配聲明之前,無法透過非限定或限定查找存取此聲明的函數。範圍。因此,從類別本身、封閉作用域或成員函數直接呼叫...
    程式設計 發佈於2024-12-22
  • ## [].forEach.call() 如何轉換 JavaScript 類別陣列結構?
    ## [].forEach.call() 如何轉換 JavaScript 類別陣列結構?
    JavaScript 中的陣列轉換:了解[].forEach.call()使用JavaScript 時,您可能會遇到使用下列程式碼片段[].forEach.call() 迭代元素列表。該技術允許使用數組函數擴展類似數組的結構,例如 NodeList。 空數組 [] 用於存取 forEach 函數,該...
    程式設計 發佈於2024-12-22
  • 如何使用 CSS 建立具有固定標題的可捲動表格?
    如何使用 CSS 建立具有固定標題的可捲動表格?
    如何使用 CSS 和固定標題製作可捲動表格在 Web 開發中,通常需要建立包含大量資料的表格需要捲動。然而,在滾動表格主體的同時保持固定的標題可能具有挑戰性。以下是實現此效果的方法:HTML 結構首先,我們必須確保 HTML 結構正確。我們有一個帶有捲軸的外部 div,一個包含表格的內部 div,表...
    程式設計 發佈於2024-12-22
  • 如何使用 JavaScript 和 Python 產生 RSA 金鑰
    如何使用 JavaScript 和 Python 產生 RSA 金鑰
    在当今的数字时代,确保敏感信息的安全至关重要。 RSA 是最广泛使用的加密技术之一,它通过允许安全通信和数据保护来帮助实现这一目标。如果您是想要学习如何生成 RSA 密钥对的初学者,本教程将指导您通过 JavaScript 和 Python 完成该过程。 什么是 RSA? RSA (...
    程式設計 發佈於2024-12-22
  • 大批
    大批
    方法是可以在物件上呼叫的 fns 數組是對象,因此它們在 JS 中也有方法。 slice(begin):將陣列的一部分提取到新數組中,而不改變原始數組。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index ...
    程式設計 發佈於2024-12-22
  • 當滑鼠懸停在子元素上時,如何僅使用 CSS 來變更父容器的背景顏色?
    當滑鼠懸停在子元素上時,如何僅使用 CSS 來變更父容器的背景顏色?
    更改子級懸停時父容器的背景顏色(僅限CSS)而有關使用CSS 選擇父元素的問題通常標記為重複,它忽略了實際解決方案的需要。特別是,當滑鼠懸停在其子容器上時更改父容器的背景顏色的問題可以透過僅 CSS 方法來解決。 指針事件和懸停: 為了達到這個效果,我們可以操作指針事件和:hover偽類:設定poi...
    程式設計 發佈於2024-12-22
  • 如何修復 MySQL 儲存表情符號時出現「字串值不正確」錯誤?
    如何修復 MySQL 儲存表情符號時出現「字串值不正確」錯誤?
    解決MySQL 表情符號儲存中的「字串值不正確」錯誤儲存包含「🎶」等表情符號的推文時,用戶可能會遇到錯誤,指出「字串值不正確...」此錯誤是由不相容的字元編碼引起的。 要解決此問題問題,資料庫和儲存推文的表的字元集需要配置為支援 Unicode 字元。操作步驟如下:修改MySQL設定檔...
    程式設計 發佈於2024-12-22
  • 如何逐行讀取大文本檔案以避免記憶體過載?
    如何逐行讀取大文本檔案以避免記憶體過載?
    逐行讀取大文本文件,不會造成內存超載處理超出內存容量的海量文本文件時,逐行讀取,不會造成記憶體超載記憶體超載變得至關重要。一種節省記憶體的方法涉及直接在文件物件上使用 for 循環。 與 open(...) 一起使用建立一個上下文管理器,該管理器在讀取後會自動關閉檔案。這是一個範例:with ope...
    程式設計 發佈於2024-12-22

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

Copyright© 2022 湘ICP备2022001581号-3