Home > Web Front-end > JS Tutorial > body text

Regarding the release of Omi v1.0.2, it officially supports code analysis for passing javascript expressions

黄舟
Release: 2017-03-21 14:15:36
Original
1441 people have browsed it

This article mainly introduces the release of Omi v1.0.2 which officially supports passing javascriptexpression, which is very good and has reference value. Friends who need it can refer to it

Written in front

The Omi framework can pass attributes to child nodes by declaring data-* on the component.

Omi has been aligned with the standard delivery method of standard DOM tags since the beginning of its design. For example:

  • The underline will be automatically converted to camel case, and when the data-page-index is passed to the sub-component, it will become this.data.pageIndex

  • data- xx is passed to the child nodes and all become strings. For example, if data-page-index="1" is passed to the child node, this.data.pageIndex is the string "1"

This will What are the limitations and problems? For example:

  • Cannot pass JSON

  • Cannot pass number type

  • Cannot pass bool type

Then supporting the passing of javascript expressions can solve these pain points.

Without further ado, let’s take a look at the colon of the artifact.

Colon mark

Look at the following example:

import Hello from 'hello.js'
Omi.makeHTML('Hello', Hello);
class App extends Omi.Component {
 render() {
 return `
 <p>
 <Hello :data-user="{ name : &#39;Dntzhang&#39;, favorite : &#39;Omi&#39; }" />
 </p>
 `
 }
}
Omi.render(new App(),"#container")
Copy after login

Add a colon in front of data-user: data-user , which means that what is passed is a js expression, which is convenient enough.

Then you can use it directly in the Hello component.

class Hello extends Omi.Component {
 render() {
 return `
 <p>
 <h1>{{user.name}} love {{user.favorite}}.</h1>
 </p>
 `
 }
}
Copy after login

You can also try printing out this.data.user in the hello component.

Passing other types

The above example shows passing JSON, other types are also supported. For example:

<Hello :data-age="18" />
 <Hello :data-xxx="1+1*2/3" />
 <Hello :data-is-girl="false" />
 <Hello :data-array-test="[1,2,3]" />
Copy after login

Complex type

Finally, let me show you a slightly more complicated case:

class Hello extends Omi.Component {
 handleClick(evt){
 alert( this.data.arrayTest[0].name)
 }
 render() {
 return `
 <ul>
 {{#arrayTest}}
 <li onclick="handleClick">{{name}}</li>
 {{/arrayTest}}
 </ul>
 `;
 }
}
Omi.makeHTML(&#39;Hello&#39;, Hello);
class App extends Omi.Component {
 render() {
 return `
 <p>
 <Hello :data-array-test="[{name:&#39;dntzhang&#39;},{name:&#39;omi&#39;},{name:&#39;AlloyTeam&#39;}]" />
 </p>
 `;
 }
}
Omi.render(new App(),"#container");
Copy after login

Of course, In subcomponents, you can also traverse using the ES6+ posture instead of using the mustache.jstemplate engine syntax.

class Hello extends Omi.Component {
 render() {
 return `
 <ul>
 ${this.data.arrayTest.map(item =>
 `<li>${item.name}</li>`
 ).join(&#39;&#39;)}
 </ul>
 `;
 }
}
Copy after login

This is why omi provides two versions, omi.js and omi.lite.js. omi.lite.js does not include the mustache.js template engine.

The above is the detailed content of Regarding the release of Omi v1.0.2, it officially supports code analysis for passing javascript expressions. For more information, please follow other related articles on the PHP Chinese website!

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