Several methods of if else implementation in AVALONJS

Source: Internet
Author: User

In the course of learning Avalonjs, found that the template does not have the if else of the wording, unlike Tempalte Ejs these, so summed up three ways to achieve, only for the use of AVALONJS classmate reference, mainly through the MS-IF expression and method to achieve.

1. Pre-start preparation

The first is to do a simple HTML as a basis

<! DOCTYPE html>

A few simple styles

. Text1{color: #f33;}. Text2{color: #33f;}. Text3{color: #3f3;}

Simple Avalonjs example initialization, where the type is what we're going to use to make judgments.

var vm = Avalon.define ({$id: "Test", type:1});     


2. How to use Ms-if

The most direct is the AVALONJS binding ms-if, but there is no ms-else such a binding, should be in the structure of the DOM to consider, so there is no such a binder bar. Then the implementation of the method is actually very simple, two of the ms-if can be achieved, That is, write two div, in different situations to do different display, in the HTML can be achieved.

<! DOCTYPE html>


3. Use an expression

The framework of MVVM supports expressions, AVALONJS is no exception, the way to do this is to write the expression in "{{}}" to judge, through the ternary operator to judge, now back to think, Avalonjs is still very powerful. Since it is written in "{{}}", Then it can be implemented in HTML.

<! DOCTYPE html>


4. Defining methods in the model

The general situation with the previous two methods should not have much problem, if it is encountered some complex judgment, you need this method, that is, in the model to customize a method to deal with, this method should be called the common method, there is a lot of space to play.

Custom a method to deal with, the problem is a lot simpler, after all, in the JS inside to do the judgment is very easy to do things, such as if else \ expression \switch, these can be used, then as long as the return content back on it, then this needs HTML and JS to co-complete.

The code for HTML is as follows:

<! DOCTYPE html>

JS code is as follows:

var vm = Avalon.define ({$id: "Test", type:1,tohtml:function (type) {if (type===0) {return "if";} Else{return "Else content";}});


5. Concluding remarks

such as AVALONJS framework, sometimes seems to limit the use of, in fact, a change of perspective is becoming more flexible, the methods described above hope to give learning avalonjs students some inspiration and reference.

The above example, written in the Runjs inside Http://runjs.cn/detail/uibw2aio. This article was posted in my personal blog, http://www.subying.com/archives/144.html.



Several methods of if else implementation in AVALONJS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.