This article mainly introduces the {view layer} applet
Applet view layer // self-study
1. data binding
The dynamic data in the data binding WXML comes from the data of the corresponding Page. Simple data binding: use the "Mustache" syntax (double braces) to package variables, which can act on: Content
{Message }}
Page ({data: {message: 'Hello MINA! '}) Component attributes (must be within double quotation marks)
Page ({data: {id: 0}) control attributes (must be within double quotation marks)
Page ({data: {condition: true}) keyword (must be within double quotation marks) true: true of the boolean type, representing the true value. False: a boolean value of the false type, indicating a false value.
Note: do not directly write checked = "false". The calculation result is a string, which is converted to the boolean type and represents the true value. You can perform simple operations within {}. the following methods are supported:
Hidden
Arithmetic operation
{A + B }}+ {c} + d
The content in Page ({data: {a: 1, B: 2, c: 3}) view is 3 + 3 + d logical judgment.
String operation
{"Hello" + name }}
Page ({data: {name: "MINA"}) data path calculation
{Object. key }{{ array [0]}
Page ({data: {object: {key: 'Hello'}, array: ['Mina']}) combinations can also be directly combined in Mustache, create a new object or array
{Item }}
Page ({data: {zero: 0}) is finally combined into an array [0, 1, 2, 3, 4] object
Page ({data: {a: 1, B: 2}) is finally combined into {for: 1, bar: 2}. you can also use the extension operator... to expand an object.
Page ({data: {obj1: {a: 1, B: 2}, obj2: {c: 3, d: 4 }}}) the final combination object is {a: 1, B: 2, c: 3, d: 4, e: 5}. if the object has the same key and value, it can also be indirectly expressed.
Page ({data: {foo: 'My-Foo', bar: 'My-bar'}) is finally combined into {foo: 'My-Foo ', bar: 'My-bar'} Note: The preceding method can be combined at will. However, if the variable name is the same, the preceding method will be overwritten, as shown in figure
Page ({data: {obj1: {a: 1, B: 2}, obj2: {B: 3, c: 4}, a: 5 }}) the final combination objects are {a: 5, B: 3, c: 6}
2. Conditional rendering
Wx: if
In MINA, we usewx:if="{{condition}}"To determine whether to render the code block:
True
You can also usewx:elifAndwx:elseTo add an else block:
1
2
3
Block wx: if
Becausewx:ifIs a control attribute and needs to be added to a tag. However, if we want to determine multiple component labels at a time, we can use one Labels encapsulate multiple components and usewx:ifControl attributes.
view1
view2
Note: It is not a component. it is just a packaging element. it does not make any rendering on the page and only accepts control attributes.
wx:ifVs
hidden
Becausewx:ifThe template in may also contain data binding.wx:ifMINA has a partial rendering process when switching the condition value, because it ensures that the condition block is destroyed or re-rendered when switching.
At the same timewx:ifYesInertIf the initial rendering condition isfalseMINA does nothing and starts partial rendering only when the condition first turns into true.
In contrast,hiddenIt is much simpler, and components are always rendered, just simple control of display and hiding.
Generally,wx:ifHigher switching consumptionhiddenHigher initial rendering consumption. Therefore, if frequent switchover is requiredhiddenBetter. if the running condition is unlikely to changewx:ifBetter.
3. list rendering, similar to volist for loop of TP
You can bind an array to the wx: for Control attribute on the component to repeatedly render the component using the data in the array. By default, the subscript variable name of the current item in the array is index by default, and the variable name of the current item in the array is item by default.
{Index }}:{ {item. message }}
Page ({items: [{message: 'foo' ,}, {message: 'bar'}]}) uses wx: for-item can be used to specify the variable name of the current element of the array. wx: for-index can be used to specify the variable name of the current underlying object of the array:
{Idx }:{{ itemName. message }}
Wx: for can also be nested. Below is a 9-9 Multiplication table
{I }}* {j }}={ {I * j }}
Block wx: for is similar to block wx: if.
Label to render a structure block containing multiple nodes. For example:
{Index }}:
{Item }}
Wx: key: If the position of the item in the list dynamically changes or a new item is added to the list, and you want the items in the list to maintain their own characteristics and status (suchContent,
You must use wx: key to specify the unique identifier of the item in the list. Wx: the key value provides a string in two forms, representing a property of item in the for loop array. The value of this property must be a unique string or number in the list, and cannot be changed dynamically. The reserved keyword * this indicates the item itself in the for loop. this indicates that the item itself must be a unique string or number, for example, when the data changes and the rendering layer is re-rendered, it will correct components with keys, and the framework will ensure that they are re-ordered, rather than re-created, to ensure that the components remain in their own state and improve the efficiency of list rendering. If the wx: key is not provided, a warning is reported. if you explicitly know that the list is static or you do not need to pay attention to its sequence, you can ignore it. Sample code:
{Item. id }}
SwitchAdd to the front
{Item }}
Add to the frontPage ({data: {objectArray: [{id: 5, unique: 'unique _ 5'}, {id: 4, unique: 'unique _ 4'}, {id: 3, unique: 'unique _ 3'}, {id: 2, unique: 'unique _ 2'}, {id: 1, unique: 'unique _ 1 '}, {id: 0, unique: 'unique _ 0'},], numberArray: [1, 2, 3, 4]}, switch: function (e) {const length = this. data. objectArray. length for (let I = 0; I <length; ++ I) {const x = Math. floor (Math. random () * length) const y = Math. floor (Math. random () * length) const temp = this. data. objectArray [x] this. data. objectArray [x] = this. data. objectArray [y] this. data. objectArray [y] = temp} this. setData ({objectArray: this. data. objectArray})}, addToFront: function (e) {const length = this. data. objectArray. length this. data. objectArray = [{id: length, unique: 'unique _ '+ length}]. concat (this. data. objectArray) this. setData ({objectArray: this. data. objectArray})}, addNumberToFront: function (e) {this. data. numberArray = [this. data. numberArray. length + 1]. concat (this. data. numberArray) this. setData ({numberArray: this. data. numberArray })}
4. applet template)
The template WXML provides a template. you can define code snippets in the template and call them in different places. The template uses the name attribute as the template name. Then
Code snippets, such:
{Index }:{{ msg }}
Time: {time }}
Use the is attribute in the template, declare the template to be used, and pass in the data required by the template, such:
Page ({data: {item: {index: 0, msg: 'This is a template', time: '2017-09-15 '}}}) is attributes can use the Mustache syntax, at runtime, you can decide which template to render:
Odd
Even
4. applet events
-
Events are the communication between the view layer and the logic layer.
-
Events can be fed back to the logic layer for processing.
-
Events can be bound to components. When an event is triggered, the corresponding event processing function in the logic layer is executed.
-
The event object can carry additional information, such as id, dataset, and touches.
-
1. how to use events 2. 3. bind an event handler to a component. 4 5 for example, bindtap. when you click this component, the corresponding event processing function is found in the corresponding Page of the Page. 6 7
Click me!
8 9 write the corresponding event handler function in the corresponding Page definition. the parameter is event. 10 11 Page ({12 tapName: function (event) {13 console. log (event) 14} 15}) 16 17 you can see the following information: 18 19 {20 "type": "tap", 21 "timeStamp": 1252, 22 "target": {23 "id": "tapTest", 24 "offsetLeft": 0, 25 "offsetTop": 0, 26 "dataset": {27 "hi ": "MINA" 28} 29}, 30 "currentTarget": {31 "id": "tapTest", 32 "offsetLeft": 0, 33 "offsetTop": 0, 34 "dataset": {35 "hi": "MINA" 36} 37}, 38 "touches": [{39 "pageX": 30, 40 "pageY": 12, 41 "clientX": 30, 42 "clientY": 12, 43 "screenX": 112, 44 "screenY": 151 45}], 46 "detail": {47 "x": 30, 48 "y ": 12 49} 50} 51 event details 53 event categories 54 55 events are classified into bubble events and non-bubble events 56 57 bubble events: when an event on a component is triggered, this event will be passed to the parent node. 58 non-bubble event: when an event on a component is triggered, the event is not transmitted to the parent node. 59 60 WXML bubble event list: 61 type trigger condition 62 touchstart finger touch 63 touchmove finger touch after moving 64 touchcancel finger touch action is interrupted, such as a call reminder, pop-up window 65 touchend finger touch action ended 66 tap finger touch left 67 longtap finger touch after more than Ms then leave 68 69 note: custom events of other components except the preceding table are non-bubble events, as shown in figure
Submit event,
Input event,
(See each component) 70 event binding 71 72 event binding is written in the same way as the component property, in the form of key and value. 73 74 key starts with bind or catch and keeps up with the event type. for example, bindtap and catchtouchstart 75 value are a string. you need to define a function with the same name in the corresponding Page. Otherwise, an error is reported when an event is triggered. 76 77 bind event binding does not prevent the bubble event from bubbling up. the catch event binding prevents the bubble event from bubbling up. 78 79 in the example below, clicking inner view will trigger handleTap1 and handleTap2 successively (because the tap event will bubble to the middle view, while the middle view will prevent the tap event from bubbling, if you click "middle view", handleTap2 is triggered. if you click "outter view", handleTap1 is triggered. 80 81
82 outer view 83
84 middle view 85
86 inner view 87
88
89
90 91 event object 92 93 unless otherwise stated, when a component triggers an event, the processing function bound to the event in the logic layer receives an Event object. 94 95 event object attribute list: 96 attribute type description 97 type String event type 98 timeStamp Integer event generation timeStamp 99 target Object trigger event component attribute value set 100 currentTarget Object current component attribute value set 101 touches array touch event, array of touch point information 102 extra information of detail Object 103 CustomEvent Custom Event Object attribute list (inheriting BaseEvent ): 104 attribute type description 105 extra information of detail Object 106 107 TouchEvent touch event Object attribute list (inheriting BaseEvent): 108 attribute type description 109 touches Array touch event, the Array 110 changedTouches Array touch event of the currently stuck touch point information on the screen, and the Array 111 112 special event of the currently changed touch point information:
The touch event in cannot be bubbling, so there is no currentTarget. 113 114 type115 common event type 116 timeStamp117 118 the number of milliseconds that occurred when the page was opened to the trigger event. 119 target120 121 source component that triggers the event. 122 attribute description 123 id124 dataset event source component of the id event source component contains 125 offsetLeft, the offset of 126 currentTarget127 128 in the coordinate system of the offsetTop event source component is the current component bound to the event. 129 attribute type description 130 id String id131 tagName String of the current component type 132 dataset Object the current component consists of a set of custom attributes starting with data-133 description: target and currentTarget can be referred to in the previous example. when you click inner view, the event objects target and currentTarget received by handleTap3 are both inner, and the event object target received by handleTap2 is inner and currentTarget is middle. 135 dataset136 137 you can define data in the component. The data will be transmitted to the SERVICE through events. Writing Method: starts with data. multiple words are separated by a hyphen (-) and cannot be capitalized (uppercase is automatically converted to lowercase), such as data-element-type, and are finally obtained in event.tar. dataset converts a hyphen to camper elementType. 138 139 example: 140 141
DataSet Test
142 143 Page ({144 bindViewTap: function (event) {145 event.tar get. dataset. alphaBeta = 1 //-the code will be converted into a camper Code 146 event.tar get. dataset. alphabeta = 2 // converts uppercase letters to lowercase letters 147} 148}) 149 150 touches151 152 touches is an array, each element is a Touch object (the touches contained in the canvas Touch event is a CanvasTouch array ). Indicates the touch point currently stuck on the screen. 153 Touch object 154 attribute type description 155 identifier Number the identifier of the Touch point 156 pageX, the distance between the pageY Number and the upper left corner of the document. the upper left corner of the document is the origin point, and the horizontal side is the x axis, the vertical direction is Y axis 157 clientX, and the clientY Number is the distance from the upper left corner of the area (excluding the navigation bar) displayed on the page. the horizontal direction is the x axis, vertical is y axis 158 CanvasTouch object 159 attribute type description special description 160 identifier Number identifier of the touch point 161 x, y Number distance from the upper left corner of the Canvas, the upper left corner of the Canvas is the origin, horizontal is the x axis, the 162 changedTouches163 164 changedTouches data format is the same as that of touches. Indicates the changed touch points, such as touchstart, touchmove, and touchcancel ). 165 detail166 167 data carried by custom events. for example, the submission event of form components carries user input, and the media error event carries error information, for details, see the definition of each event in the component definition.
5. small program reference
{{text}} A template B template body header footer
For more articles about the {view layer} applet, please follow the PHP Chinese site!