Home > Web Front-end > JS Tutorial > riot.js learning [8] Observer

riot.js learning [8] Observer

黄舟
Release: 2017-01-16 16:22:20
Original
1264 people have browsed it

Because Riot is developed based on custom tags [components]. All properties and methods inside the tag are private, and it is difficult to access the tag content from the outside.

If you want to access the content in the tag, there are several thankless methods:

1. Window global variable method

[code]<script type="riot/tag">
    <todo>
        <h1>{ title }</h1>

        this.title = opts.title || "da宗熊";
        // window.TODO劫持现在的this对象
        window.TODO = this;
    </todo>
</script>
Copy after login

Advantages:

Very violent and simple, all content of the tag content can be accessed through window.TODO.

Disadvantages:

Not applicable when the page has multiple identical tags

2. Return value of riot.mount

[code]<script type="riot/tag">
    <todo>
        <h1>{ title }</h1>

        this.title = opts.title || "da宗熊";
    </todo>
</script>
<script>
    // 这个todo,返回的是个数组!!!
    var todo = riot.mount("todo");
    // todo = [tag];
</script>
Copy after login
Returned by riot.mount , is an array. To get the content of the first "todo" tag, you need to do this: var todo1 = todo[0];

Advantages:

There is a way to distinguish multiple tags

Disadvantages:

There is a serious dependence on the order in which custom tags are placed. And the return value of the custom tag loaded back through the script's src is null.

Only by manually calling the loading method and compiling can you access the context:
[code]riot.compile("todo.tag", function(){ 
    /*才能获取到返回的内容*/
    var todo = riot.mount("todo")[0];
});
Copy after login

riot.observable

None of the above is king. In Riot, it is built-in Publisher, we only need to use simple code to create a jQuery-like publisher:

[code]var opts = riot.observable({
    // some code...
});
Copy after login

The publisher generated through riot.observable has common methods such as on, off, trigger, one [familiar A classmate of jq, you should know what’s going on by looking at the name].

Take opts as an example. Here is a brief description of each method:

[code]// 监听事件
opts.on("event1", function(data1, data2){
    // 监听event1事件
    // data1和data2是trigger传入的参数
    // data1 = 1, data2 = 2
    console.log(data1, data2);
});

// 发布一个事件
// 该事件带有 1和2 作为参数
// 上面的on("event1")的回调fn将会执行
opts.trigger("event1", 1, 2);

// 解除event1 的所有监听,第二个参数可选
// 如果有第二个参数[function],则只解绑该函数
opts.off("event1");

// one与on类似,只是one如果执行过一次,就自动解除绑定
opts.one("event1", function(data1){
    console.log(data1);
});
opts.trigger("event1", 1, 2);
Copy after login

The final output will be:

[code]1 2
1
Copy after login

Riot recommends that we use opts and observable to solve internal and external communications The problem. Let’s take an example:

[code]<!Doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Riot.js 事件监听</title>
    <script type="text/javascript" src="riot.js"></script>
    <script type="text/javascript" src="compiler.js"></script>
</head>
<body>
    <todo></todo>
</body>
<script type="riot/tag">
    <todo>
        <a href="javascript:;" onclick={ opts.login }>登录</a>

        opts.on("outside", function(value){
            console.log("outside value:" + value);
        });
    </todo>
</script>
<script type="text/javascript">
var opts = riot.observable({
    login: function(params){
        // 这里的this被todo更改了..
        opts.trigger("outside", "登录成功...");
    }
});
riot.mount("todo", opts);

</script>
</html>
Copy after login

After clicking to log in, the effect is as follows:

riot.js learning [8] Observer

Through event monitoring and publishing, you can have a good contact with internal and external communication issues, and at the same time , you can also

restrict access to certain data to a great extent.

However, opts are largely coupled to tags.

The label needs to know that opts will publish "outside" and listen;

opts needs to know that the label will call its login method;

If there is no good team Norm, this would be a disaster.

SO:

No matter which mode or method, it has its own applicable scenarios. If used correctly, it will be the finishing touch; if used incorrectly, it will be difficult to achieve even a single step. Think more before taking action

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


Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template