”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 绑定和模板:Peasy-UI 系列的一部分

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

发布于2024-11-08
浏览:712

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]删除
最新教程 更多>
  • 变化检测从 Angular zone.js) 到 Angular (provideExperimentalZonelessChangeDetection) 的演变
    变化检测从 Angular zone.js) 到 Angular (provideExperimentalZonelessChangeDetection) 的演变
    更改检测是 Angular 的一个基本方面,负责识别和更新 DOM 中因数据修改或用户交互而发生更改的部分。此过程可确保 UI 与底层数据保持一致,从而增强用户体验和应用程序性能。 Zone.js 的作用 从历史上看,Angular 一直依赖 Zone.js 来实现其变更检测机制。 ...
    编程 发布于2024-11-08
  • 使用 Streamlit 深入研究数据应用程序
    使用 Streamlit 深入研究数据应用程序
    The ability to quickly develop and deploy interactive applications is invaluable. Streamlit is a powerful tool that enables data scientists and develo...
    编程 发布于2024-11-08
  • Go 中的匿名结构:什么时候在 `map[string]struct{}` 中指定类型是多余的?
    Go 中的匿名结构:什么时候在 `map[string]struct{}` 中指定类型是多余的?
    匿名结构:揭示 struct{}{} 和 {} 之间的差异在 Go 中,声明字符串到匿名结构映射可以通过两种方式完成:var Foo = map[string]struct{}{ "foo": struct{}{}, }var Foo = map[string]struc...
    编程 发布于2024-11-08
  • 如何在不离开当前页面的情况下打开下载窗口?
    如何在不离开当前页面的情况下打开下载窗口?
    在不离开当前页面的情况下打开下载窗口的最简单方法要在不离开当前页面或打开弹出窗口的情况下打开下载对话框,使用以下跨浏览器 JavaScript:window.location.assign(url);此方法不会打开新窗口或选项卡,即使在 Internet Explorer 6 中也能正常工作。通过将...
    编程 发布于2024-11-08
  • 如何在 C++ 中使用虚拟模板方法实现多态性?
    如何在 C++ 中使用虚拟模板方法实现多态性?
    C 虚拟模板方法在 C 中,将静态时间多态性(模板)与运行时多态性结合起来可能具有挑战性。这在以下抽象类中很明显:class AbstractComputation { public: template <class T> virtual void setData...
    编程 发布于2024-11-08
  • 多线程:工程师的关键概念 - 第 1 部分
    多线程:工程师的关键概念 - 第 1 部分
    了解关键的多线程概念对于软件开发人员至关重要,因为它不仅可以增强技能,还可以直接影响应用程序开发、可扩展性和软件解决方案的整体质量。 原子性 在多线程上下文中,原子操作确保一个线程可以执行一系列操作而不会被其他线程中断。多个线程可能会尝试同时读取或写入共享数据。如果没有原子性,并发...
    编程 发布于2024-11-08
  • 后端开发路线图
    后端开发路线图
    对于所有有抱负的后端开发人员,我知道导航您的学习道路是多么令人畏惧。这就是为什么我很高兴能够提供详细的路线图来帮助您掌握用于后端开发的 JavaScript!? 1️⃣ 从基础开始:熟悉 JavaScript 基础知识。理解变量、函数和控制结构至关重要。 2️⃣ 深入了解 Node.js:这个强大的...
    编程 发布于2024-11-08
  • 如何在Go中调用Linux共享库函数?
    如何在Go中调用Linux共享库函数?
    在 Go 中调用 Linux 共享库函数在此问题中,开发人员寻求从共享对象 (.so) 调用函数的帮助他们的 Go 代码中的文件。 Python 中的 ctypes 包可以访问 C 函数,可以作为所需的功能。使用 cgo 进行静态共享库加载静态调用函数在编译时已知共享库,可以使用 cgo 包。以下是...
    编程 发布于2024-11-08
  • Java 中的字符串文字是否会被垃圾收集?
    Java 中的字符串文字是否会被垃圾收集?
    字符串文字的垃圾收集Q1:如果一个字符串在编译时被定义为文字(例如,String str = "java"),它会被垃圾收集吗?通常不会。代码对象包含对文字 String 对象的引用,使它们保持可访问性。垃圾回收仅在代码对象变得不可访问时发生。Q2:如果使用 intern 方法(...
    编程 发布于2024-11-08
  • 跨平台使用 RSA 加密和解密保护数据
    跨平台使用 RSA 加密和解密保护数据
    RSA加密简介 在当今的数字环境中,保护敏感数据对于个人和组织都至关重要。 RSA (Rivest-Shamir-Adleman) 加密作为保护数据的强大解决方案脱颖而出。它是一种非对称加密算法,这意味着它使用一对密钥:用于加密的公钥和用于解密的私钥。 RSA 加密的主要好处之一是...
    编程 发布于2024-11-08
  • 如何利用导师在数字营销中实现职业发展
    如何利用导师在数字营销中实现职业发展
    在当今快速发展的数字营销环境中,保持领先趋势并提高您的技能至关重要。加速该领域职业发展的最有效方法之一是利用指导。无论您是刚刚起步还是希望晋升为领导角色,数字营销导师都可以提供宝贵的指导和见解,帮助您实现职业目标。本文探讨了指导如何塑造您的职业轨迹,并提供了充分利用指导经验的策略。 ...
    编程 发布于2024-11-08
  • 将 Cloudinary 集成到 Next.js 应用程序中
    将 Cloudinary 集成到 Next.js 应用程序中
    了解 Cloudinary 及其定价。 1. 创建一个Cloudinary账户 如果您没有帐户,请在 Cloudinary 注册并创建一个新帐户。 2.安装Cloudinary SDK 您可以使用npm或yarn安装Cloudinary SDK: npm instal...
    编程 发布于2024-11-08
  • 我们如何在 CSS 中对后代元素进行分组?
    我们如何在 CSS 中对后代元素进行分组?
    用于后代分组的难以捉摸的 CSS 选择器虽然 CSS 为样式元素提供了强大的选项,但一个值得注意的缺失是轻松对后代进行分组的能力。当对 HTML 表格等复杂元素进行样式设计时,这种限制变得很明显,其中为列标题和单元格分配相同的样式需要繁琐的选择器。后代样式设计的困境考虑下面的表示例:<tabl...
    编程 发布于2024-11-08
  • ## 如何在 PHP 中可靠地确定目录是否为空?
    ## 如何在 PHP 中可靠地确定目录是否为空?
    在 PHP 中验证目录是否为空确定目录是否为空可能是各种 Web 开发场景中的一项重要任务。但是,某些脚本可能会遇到问题,即输出错误地建议空或非空目录,尽管其中存在或不存在文件。原始脚本提供的脚本尝试使用以下代码检查目录是否为空:$q = (count(glob("$dir/*"...
    编程 发布于2024-11-08

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3