| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,9 @@ | |||
| 48 | 48 | * [3.2.1 Program](chapter-3/3.2.1 Program.md) | |
| 49 | 49 | * [3.2.2 Page](chapter-3/3.2.2 Page.md) | |
| 50 | 50 | * [3.3 Component](chapter-3/3.3 Component.md) | |
| 51 | + * [3.4 API](chapter-3/3.4 API.md) | ||
| 52 | + * [3.5 Events](chapter-3/3.5 Events.md) | ||
| 53 | + * [3.6 Compatibility](chapter-3/3.6 Compatibility.md) | ||
| 51 | 54 | ||
| 52 | 55 | ||
| 53 | 56 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + ## 3.4 API | ||
| 2 | + | ||
| 3 | + Host environment for mini programs offers various APIs from WeChat Native. Such as `wx.navigateTo()` in previous example, it stores current page and redirects page to a new one. `wx` here is the global Object from host environment, nearly all APIs are on this object (except special constructors like `Page`, `App`). So "API" in the chapter mainly means methods under `wx`. | ||
| 4 | + | ||
| 5 | + APIs can be categorized to Network, Media, File, Cache, Location, Device, UI, Element and other ones. Here is the common rule of API calling: | ||
| 6 | + | ||
| 7 | + 1. `wx.on*` is some event listeners, accept a callback as parameter. When the event is triggered, the callback is run | ||
| 8 | + 2. Unless annotated, most APIs are async, accepting an Object as parameter | ||
| 9 | + 3. The parameter Object usually supports three callbacks: `success`, `fail`, `complete`, as shown in code snippet 3-17 | ||
| 10 | + 4. `wx.get*` are APIs to get data from host environment. | ||
| 11 | + 5. `wx.set*` are APIs to write data to host environment. | ||
| 12 | + | ||
| 13 | + *code snippet 3-17 network request through `wx.request` | ||
| 14 | + ```js | ||
| 15 | + wx.request({ | ||
| 16 | + url: 'test.php', | ||
| 17 | + data: {}, | ||
| 18 | + header: { 'content-type': 'application/json' }, | ||
| 19 | + success: function(res) { | ||
| 20 | + // when http request suceeds | ||
| 21 | + console.log(res.data) | ||
| 22 | + }, | ||
| 23 | + fail: function() { | ||
| 24 | + // when error happens in request | ||
| 25 | + }, | ||
| 26 | + complete: function() { | ||
| 27 | + // when request completes, either succeeds or fails | ||
| 28 | + } | ||
| 29 | + }) | ||
| 30 | + ``` | ||
| 31 | + | ||
| 32 | + *table 3-9 API callbacks* | ||
| 33 | + | ||
| 34 | + parameter name | type | mandatory | description | ||
| 35 | + success | Function | no | callbak when API suceeds | ||
| 36 | + fail | Function | no | callback when API fails | ||
| 37 | + complete | Function | no | callback when API complets | ||
| 38 | + | ||
| 39 | + Attention most APIs are async, and some API will call out native views, which leads to `onHide()` in `Page` being called . | ||
| 40 | + | ||
| 41 | + There are many many APIs, as the host environment envolves new APIs are added. Here we don't explain the details of all APIs, but the common skills. More details could be found in https://mp.weixin.qq.com/debug/wxadoc/dev/api/. In Chapter 4 we will show more use cases. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,144 @@ | |||
| 1 | + ## 3.5 Events | ||
| 2 | + | ||
| 3 | + ### 3.5.1 What is an Event | ||
| 4 | + | ||
| 5 | + User interace need to be interacted by users, for example, users may click some buttons or long press some areas, these actions should be notified to developers, and the new state should be reflected. | ||
| 6 | + | ||
| 7 | + Sometimes the feedback is not triggered by users, such as the progress bar when video is being played, these changes should also be notified. | ||
| 8 | + | ||
| 9 | + In mini programs, these components' updates triggered by users' interactions are defined as "events" passed from rendering layer to logic layer, as the following image shows: | ||
| 10 | + | ||
| 11 | +  | ||
| 12 | + *Figure 3-7 redering layer generates and passes events to logic layer* | ||
| 13 | + | ||
| 14 | + Here is a simple mini program. | ||
| 15 | + | ||
| 16 | + *code snippet 3-18 exmaple of event handling* | ||
| 17 | + ```xml | ||
| 18 | + <!-- page.wxml --> | ||
| 19 | + <view id="tapTest" data-hi="WeChat" bindtap="tapName"> Click me! </view> | ||
| 20 | + | ||
| 21 | + // page.js | ||
| 22 | + Page({ | ||
| 23 | + tapName: function(event) { | ||
| 24 | + console.log(event) | ||
| 25 | + } | ||
| 26 | + }) | ||
| 27 | + | ||
| 28 | + ``` | ||
| 29 | + | ||
| 30 | + Event listener `tapName()` are attached to components through `bindtap` and it is declared in Page constructors. When users tap the view area, tap events are riggered, and `tapName()` will be called with the event as paremter. | ||
| 31 | + | ||
| 32 | + ### 3.5.2 Event Type and event Objects | ||
| 33 | + | ||
| 34 | + Because different components have different state, here component-related events are not discussed, those events can be found at: https://mp.weixin.qq.com/debug/wxadoc/dev/component/. | ||
| 35 | + | ||
| 36 | + Common event types are listed here: | ||
| 37 | + | ||
| 38 | + *Table 3-10 common event types* | ||
| 39 | + type | condition | ||
| 40 | + -----|-------- | ||
| 41 | + touchstart | finger touchs screen | ||
| 42 | + touchmove | finger moves after touching | ||
| 43 | + touchcancel | finger move is disrupted by income call, native modal, .etc | ||
| 44 | + touchend | finger move ends | ||
| 45 | + tap | finger leaves right after first touch | ||
| 46 | + longpress | finger touches, and leaves after 350ms. If this event is triggered, tap events will not occur | ||
| 47 | + longtap | finger touches, and leaves after 350ms. (`longpress` is recommended to use) | ||
| 48 | + transitionend | triggered after WXSS transition or `wx.createAnimation` | ||
| 49 | + animationstart | triggered before the first paint of WXSS animation | ||
| 50 | + animationiteration | triggered after one frame of animation is rendered | ||
| 51 | + animationend | triggered after one WXSS animation is done | ||
| 52 | + | ||
| 53 | + The event passed to event handler has following properties. | ||
| 54 | + | ||
| 55 | + *Table 3-11 event object properties* | ||
| 56 | + | ||
| 57 | + property | type | description | ||
| 58 | + ---------|-------|-------- | ||
| 59 | + type | String | event type | ||
| 60 | + timeStamp | Integer | time passed from page opened to event triggered(in miliseconds) | ||
| 61 | + target | Object | property set of the component where event is triggered | ||
| 62 | + currentTarget | Object | property set of current component | ||
| 63 | + detail | Object | extra information | ||
| 64 | + touches | Array | touch events, current active touch points | ||
| 65 | + changedTouches | Array | touch events, currently changed touch points | ||
| 66 | + | ||
| 67 | + Attention to the difference between `target` and `currentTarget`. `currentTarget` is the component where event listener is attached, and `target` the source component where event is triggered. | ||
| 68 | + | ||
| 69 | + *code snippet 3-19 event object example* | ||
| 70 | + ```xml | ||
| 71 | + <!-- page.wxml --> | ||
| 72 | + <view id="outer" catchtap="handleTap"> | ||
| 73 | + <view id="inner">click me</view> | ||
| 74 | + </view> | ||
| 75 | + ``` | ||
| 76 | + | ||
| 77 | + ```js | ||
| 78 | + // page.js | ||
| 79 | + Page({ | ||
| 80 | + handleTap: function(evt) { | ||
| 81 | + // when click inner element | ||
| 82 | + // evt.target is inner view component | ||
| 83 | + // evt.currentTarget is outer view where handleTap is attached | ||
| 84 | + // evt.type == “tap” | ||
| 85 | + // evt.timeStamp == 1542 | ||
| 86 | + // evt.detail == {x: 270, y: 63} | ||
| 87 | + // evt.touches == [{identifier: 0, pageX: 270, pageY: 63, clientX: 270, clientY: 63}] | ||
| 88 | + // evt.changedTouches == [{identifier: 0, pageX: 270, pageY: 63, clientX: 270, clientY: 63}] | ||
| 89 | + } | ||
| 90 | + }) | ||
| 91 | + ``` | ||
| 92 | + | ||
| 93 | + The detailed property paramters about `target` and `currentTarget` are listed here: | ||
| 94 | + | ||
| 95 | + *Table 3-12 properties of target and currentTarget* | ||
| 96 | + | ||
| 97 | + property | type | description | ||
| 98 | + ---------|------|---------- | ||
| 99 | + id | String | current component's id | ||
| 100 | + tagName | String | current component's type | ||
| 101 | + dataset | Object | data attributes of current component | ||
| 102 | + | ||
| 103 | + *Table 3-3 peroperties of touch and changedTouches* | ||
| 104 | + property | type | description | ||
| 105 | + -------|------|---------- | ||
| 106 | + identifier | Number | id of touch point | ||
| 107 | + pageX, pageY | Number | distance to top left corner. Top left corner is the origin point, horizontal direction as x-axis, vertical direction as Y-axis | ||
| 108 | + clientX, clientY | Number | similar to above one, but distance is relative screen area (without the navigation bar) | ||
| 109 | + | ||
| 110 | + | ||
| 111 | + ### 3.5.3 Event binding, bubbling and capturing | ||
| 112 | + | ||
| 113 | + Event binding is done in the same format as `key="value"`. | ||
| 114 | + | ||
| 115 | + 1. key should start with `bind` or `catch`, followed by event type, such as `bindtap`, `catchtouchstart`. From version 1.5.0, semicolon could be inserted , like `bind:tap`, `catch:touchstart`, they are the same. You can also `capture-` at the beginning to handle events as capuring phase. value is String, which is the method name defined in Page constructor, error occurs if method is not defined. `bind` means bubbling phase, and `capture-bind` means capturing phase, the order is : | ||
| 116 | + | ||
| 117 | +  | ||
| 118 | + *Figure 3-8 bubbling and capuring phase* | ||
| 119 | + | ||
| 120 | + In the following example, tapping inner view will call `handleTap1`, `handleTap2`, `handleTap3` and `handleWTap4` in the right order. | ||
| 121 | + | ||
| 122 | + *code snippet 3-20 use capture to set phase* | ||
| 123 | + ```xml | ||
| 124 | + <view id="outer" bind:tap="handleTap4" capture-bind:tap="handleTap1"> | ||
| 125 | + outer view | ||
| 126 | + <view id="inner" bind:tap="handleTap3" capture-bind:tap="handleTap2"> | ||
| 127 | + inner view | ||
| 128 | + </view> | ||
| 129 | + </view> | ||
| 130 | + ``` | ||
| 131 | + | ||
| 132 | + `bind` will not stop event bubbling, `catch` is used to do that. If change `capture-bind:tap="handleTap1"` to `capture-catch:tap="handleTap1"`, tapping inner view will only trigger `handleATap1`, becase `catch` stops the bubbling. | ||
| 133 | + | ||
| 134 | + *code snippet 3-21 event capturing and bubbling* | ||
| 135 | + ```xml | ||
| 136 | + <view id="outer" bind:tap="handleTap4" capture-catch:tap="handleTap1"> | ||
| 137 | + outer view | ||
| 138 | + <view id="inner" bind:tap="handleTap3" capture-bind:tap="handleTap2"> | ||
| 139 | + inner view | ||
| 140 | + </view> | ||
| 141 | + </view> | ||
| 142 | + ``` | ||
| 143 | + | ||
| 144 | + Attention that all other events(except above ones) are default to non-bubbling ones, unless special notice is addressed, such as `submit` in `<form/>`, `input` in `<input/>`, `scroll` in `<scroll-view/>`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,54 @@ | |||
| 1 | + ## 3.6 Compatibility | ||
| 2 | + | ||
| 3 | + Host environment for mini programs are always envolving, providing more and more possibilities to developers, thus your mini programs are run in different versions of host environments. In order to provide all environments the same service, we need to know how to handle compatibility problems. | ||
| 4 | + | ||
| 5 | + To handle problems across differnt devices, we can use `wx.getSystemInfo` or `wx.getSystemInfoSync` to retrieve brand, OS version, WeChat Native Version or Mini Program SDK version .etc, and offer localized service. | ||
| 6 | + | ||
| 7 | + *code snippet 3-22 get host info from `wx.getSystemInfoSync`* | ||
| 8 | + | ||
| 9 | + ```js | ||
| 10 | + wx.getSystemInfoSync() | ||
| 11 | + /* | ||
| 12 | + { | ||
| 13 | + brand: "iPhone", // branch | ||
| 14 | + model: "iPhone 6", // device model | ||
| 15 | + platform: "ios", // os | ||
| 16 | + system: "iOS 9.3.4", // os version | ||
| 17 | + version: "6.5.23", // wechat version | ||
| 18 | + SDKVersion: "1.7.0", // mini program sdk version | ||
| 19 | + language: "zh_CN", // language setting in wechat | ||
| 20 | + pixelRatio: 2, // device pixel ratio | ||
| 21 | + screenHeight: 667, // screen height | ||
| 22 | + screenWidth: 375, // screen width | ||
| 23 | + windowHeight: 667, // usable window height | ||
| 24 | + windowWidth: 375, // usable window width | ||
| 25 | + fontSizeSetting: 16 // font size setting | ||
| 26 | + } | ||
| 27 | + */ | ||
| 28 | + ``` | ||
| 29 | + | ||
| 30 | + You can check whether some methods exists to improve compatibility. | ||
| 31 | + | ||
| 32 | + *code 3-23 check API's existence* | ||
| 33 | + | ||
| 34 | + ```js | ||
| 35 | + if (wx.openBluetoothAdapter) { | ||
| 36 | + wx.openBluetoothAdapter() | ||
| 37 | + } else { | ||
| 38 | + // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示 | ||
| 39 | + wx.showModal({ | ||
| 40 | + title: 'Alert', | ||
| 41 | + content: 'Please update WeChat to use this feature' | ||
| 42 | + }) | ||
| 43 | + } | ||
| 44 | + ``` | ||
| 45 | + | ||
| 46 | + `wx.canIUse` is provided to check whether some API is available, its parameter should follow `${API}.${method}.${param}.${options}` or `${component}.${attribute}.${option}` | ||
| 47 | + | ||
| 48 | + Segments of the parameters are: | ||
| 49 | + | ||
| 50 | + * `${API}`, API name | ||
| 51 | + * `${method}`, way of calling, `return`, `success`, `object` or `callback` | ||
| 52 | + * `${param}`, parameter or return value | ||
| 53 | + * `${options}`, optional paramters | ||
| 54 | + * `${component}`, component name | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments