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-ifThe 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 expressionThe 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 modelThe 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 remarkssuch 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 is written in Runjs. This article was posted in my personal blog, http://www.subying.com/archives/144.html.
Several methods of if else implementation in AVALONJS