Home Web Front-end JS Tutorial riot.js learning [6] Chowder 2

riot.js learning [6] Chowder 2

Jan 16, 2017 pm 04:17 PM

Building method

Riot defaults to building DOM content through "custom tags", but you can also specify which "custom tag" to use to build DOM through the attribute "riot-tag".

For example:

[code]    <div riot-tag="todo" title="da宗熊"></div>
    等同于:
    <todo title="da宗熊"></todo>
Copy after login

Alternatively, you can also specify the built elements in riot.mount:

[code]<div id="content"></div>
<script>
    // 给id="content"的元素,构建todo的内容
    // 第一个参数,可以是dom元素,too
    riot.mount("#content", "todo");
</script>
Copy after login

Style

Customize the label in Riot , you can use the style tag to write styles for components. Riot will insert all style content into the head tag, so there is no need to worry about styles being defined repeatedly.

[code]<!Doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="riot.js"></script>
    <script type="text/javascript" src="compiler.js"></script>
</head>
<body>

    <todo></todo>
    <todo></todo>

</body>

<!-- 最前面一定要有空格或TAB -->
<script type="riot/tag">
    <todo>
        <label>da宗熊</label>
        <!-- 自定义样式 -->
        <style>
            label{color:#ccc;}
        </style>
    </todo>
</script>

<script type="text/javascript">
    riot.mount("todo");
</script>
</html>
Copy after login

Final generation:

riot.js learning [6] Chowder 2

Although the todo tag was called twice, only one style was finally generated.

each loop

To implement lists like ul and ol, looping is absolutely necessary. Riot has built-in each attribute, which is used to loop to generate the content of "class list" [We are not just You can traverse lists and even objects].

[code]<!Doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="riot.js"></script>
    <script type="text/javascript" src="compiler.js"></script>
</head>
<body>

    <todo></todo>

</body>

<!-- 最前面一定要有空格或TAB -->
<script type="riot/tag">
    <todo>
        <!-- 遍历列表 -->
        <ul each={ item in list }>
            <li>{ item }</li>
        </ul>
        <!-- 遍历对象 -->
        <ul each={ key,value in obj }>
            <li>{ key }:{ value }</li>
        </ul>
        // 列表内容
        this.list = [
            "内容1",
            "内容2",
            "内容3"
        ];
        // 对象内容
        this.obj = {
            name: "da宗熊",
            age: 26
        };
    </todo>
</script>

<script type="text/javascript">
    riot.mount("todo");
</script>
</html>
Copy after login

is generated as follows:

riot.js learning [6] Chowder 2

If the content of the list is an object, Riot also provides a quick access method:

[code]<script type="riot/tag">
    <todo>
        <ul each={ list }>
            <!-- 这里的 context 已经是 list
 咧 -->
            <li>{ title }</li>
        </ul>
        // 列表内容
        this.list = [
            {title: "第一个"},
            {title: "第二个"}
        ];
    </todo>
</script>
Copy after login

Result As follows:

riot.js learning [6] Chowder 2

It can be seen that the context [this object] in the bracket expression [{ expression }] is no longer the context of the label, but has become the current traversal object of the current item.

If we need to access the context of the label in the each loop, we can access it through the "parent" keyword.

For example:

[code]<script type="riot/tag">
    <todo>
        <ul each={ items }>
            <li>
                <span>{ title }</span>
                <a onclick={ parent.remove }>删除</a>
            </li>
        </ul>

        this.items = [{title: "da宗熊"}];
        remove(e){
            // 获取到当前点击, each循环中的item
            // 这里就是获取到了 {title: "da宗熊"} 这个对象
            var item = e.item;
            var index = this.items.indexOf(item);
            // 把当前项删掉
            // 数组的slice,push等操作,自带了this.update操作,SO,这里省了this.update调用
            this.items.splice(index, 1);
        }
    </todo>
</script>
Copy after login

In the event object, you can access the current object in the current each loop through event.item.

The above is the content of riot.js learning [6] Chowder 2. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

See all articles