You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ji...@apache.org on 2017/02/20 06:41:13 UTC

[24/50] [abbrv] incubator-weex git commit: * [ios] add doc for component method

* [ios] add doc for component method


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/9b69a04b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/9b69a04b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/9b69a04b

Branch: refs/heads/master
Commit: 9b69a04be60dae610cf36462880505917d3034cf
Parents: 0e37541
Author: acton393 <zh...@gmail.com>
Authored: Thu Feb 16 14:30:33 2017 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Thu Feb 16 14:30:33 2017 +0800

----------------------------------------------------------------------
 .../cn/v-0.10/advanced/extend-to-android.md     |  27 +++-
 doc/source/cn/v-0.10/advanced/extend-to-ios.md  | 154 ++++++++++++++++++-
 doc/source/v-0.10/advanced/extend-to-android.md |  57 +++++--
 doc/source/v-0.10/advanced/extend-to-ios.md     |  39 +++++
 4 files changed, 259 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/9b69a04b/doc/source/cn/v-0.10/advanced/extend-to-android.md
----------------------------------------------------------------------
diff --git a/doc/source/cn/v-0.10/advanced/extend-to-android.md b/doc/source/cn/v-0.10/advanced/extend-to-android.md
index a1d760a..f794f73 100644
--- a/doc/source/cn/v-0.10/advanced/extend-to-android.md
+++ b/doc/source/cn/v-0.10/advanced/extend-to-android.md
@@ -141,5 +141,30 @@ public class ImageAdapter implements IWXImgLoaderAdapter {
   }
 }
 ```
-
+#### \u7ec4\u4ef6\u65b9\u6cd5\u652f\u6301
+\u4eceWeexSDK 0.9.5\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u7ec4\u4ef6\u65b9\u6cd5
+
+- \u5728\u7ec4\u4ef6\u4e2d\u5982\u4e0b\u58f0\u660e\u4e00\u4e2a\u7ec4\u4ef6\u65b9\u6cd5
+
+ ```java
+ @JSMethod
+ public void focus(){
+ 	//method implementation
+ }
+ ```
+- \u6ce8\u518c\u7ec4\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u5728weex \u6587\u4ef6\u4e2d\u8c03\u7528
+  
+  ```html
+	<template>
+ 		<mycomponent id='mycomponent'></mycomponent>
+	</template>
+	<script>
+   		module.exports = {
+    		created: function() {
+    			this.$el('mycomponent').focus();
+    		}
+   		}
+	</script>
+	```
+	
 \u6ce8:\u5de5\u7a0b\u8981\u6dfb\u52a0\u4f9d\u8d56 `compile 'com.squareup.picasso:picasso:2.5.2'`

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/9b69a04b/doc/source/cn/v-0.10/advanced/extend-to-ios.md
----------------------------------------------------------------------
diff --git a/doc/source/cn/v-0.10/advanced/extend-to-ios.md b/doc/source/cn/v-0.10/advanced/extend-to-ios.md
index 3562035..54b855b 100644
--- a/doc/source/cn/v-0.10/advanced/extend-to-ios.md
+++ b/doc/source/cn/v-0.10/advanced/extend-to-ios.md
@@ -69,9 +69,10 @@ Weex SDK \u53ea\u63d0\u4f9b\u6e32\u67d3\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u7684\u80fd\u529b\uff0c\u5982\u679c\u4f60\u9700\u8981 \u50cf\u7f51\u7edc
    
    Weex SDK\u6ca1\u6709 \u56fe\u7247\u4e0b\u8f7d\uff0cnavigation \u64cd\u4f5c\u7684\u80fd\u529b\uff0c\u8bf7\u5927\u5bb6\u81ea\u5df1\u5b9e\u73b0\u8fd9\u4e9b protocol
 
-4. **WXImgLoaderProtocol**  
+### handler \u6269\u5c55
+   **WXImgLoaderProtocol**  
 
-   weexSDK \u6ca1\u6709\u56fe\u7247\u4e0b\u8f7d\u7684\u80fd\u529b\uff0c\u9700\u8981\u5b9e\u73b0 WXImgLoaderProtocol,\u53c2\u8003\u4e0b\u9762\u7684\u4f8b\u5b50
+   weexSDK \u6ca1\u6709\u63d0\u4f9b\u56fe\u7247\u4e0b\u8f7d\u7684\u80fd\u529b\uff0c\u9700\u8981\u5b9e\u73b0 WXImgLoaderProtocol,\u53c2\u8003\u4e0b\u9762\u7684\u4f8b\u5b50
    
    ```object-c
    WXImageLoaderProtocol.h
@@ -127,3 +128,152 @@ Weex SDK \u53ea\u63d0\u4f9b\u6e32\u67d3\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u7684\u80fd\u529b\uff0c\u5982\u679c\u4f60\u9700\u8981 \u50cf\u7f51\u7edc
    
    [WXSDKEngine registerHandler:[WXImgLoaderDefaultImpl new] withProtocol:@protocol(WXImgLoaderProtocol)]
    ```
+
+#### Component \u6269\u5c55
+   \u867d\u7136WeexSDK\u4e2d\u6709\u63d0\u4f9b\u5185\u7f6e\u7684\u4e00\u4e9bComponent\uff0c\u4f46\u8fd9\u6709\u53ef\u80fd\u5e76\u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002\u5728\u4e4b\u524d\u4f60\u53ef\u80fd\u5df2\u7ecf\u5199\u4e86\u4e00\u4e9b\u5f88\u9177\u70abnative\u7684\u7ec4\u4ef6\uff0c\u60f3\u5305\u88c5\u4e00\u4e0b\uff0c\u5bfc\u5165\u5230Weex\u4e2d\uff0c\u56e0\u6b64\u6211\u4eec\u63d0\u4f9b\u4e86\u8ba9\u5f00\u53d1\u8005\u5b9e\u73b0\u81ea\u5df1\u7684native Component   
+   \u4e0b\u9762\u5c06\u4ee5WeexSDK \u4e2d\u5df2\u7ecf\u5b58\u5728\u7684 Component\uff1a`image`\u4e3a\u4f8b\u5b50\uff0c\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u6784\u5efa\u4e00\u4e2anative Component.
+   \u5047\u8bbe\u4f60\u5df2\u7ecf\u4e86\u89e3IOS\u5f00\u53d1  
+   1. \u6ce8\u518c Component  
+      \u6ce8\u518c\u4e00\u4e2acomponent\u6bd4\u8f83\u7b80\u5355\uff0c\u8c03\u7528 `WXSDKEngine` \u4e2d\u7684 `registerComponent:withClass:`\u65b9\u6cd5\uff0c\u4f20\u5165\u7ec4\u4ef6\u7684\u6807\u7b7e\u540d\u79f0\uff0c\u8fd8\u6709\u5bf9\u5e94\u7684class  
+      \u7136\u540e\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a `WXImageComponent` \u8868\u793a`image`\u7ec4\u4ef6\u7684\u5b9e\u73b0     \u5728.we \u6587\u4ef6\u4e2d\uff0c\u53ea\u9700\u8981\u5199 
+          <image></image>  
+   2. \u6dfb\u52a0\u5c5e\u6027   
+      \u73b0\u5728\u6211\u4eec\u8981\u505a\u4e00\u4e9b\u8ba9image component\u66f4\u52a0\u5f3a\u5927\u7684\u4e8b\u60c5\u3002\u65e2\u7136\u4f5c\u4e3a\u4e00\u4e2a\u56fe\u7247\u7684component\uff0c\u90a3\u5b83\u5e94\u8be5\u8981\u6709\u6e90\uff0c\u7ed9\u4ed6\u52a0\u4e0a\u4e00\u4e2a `src`\u7684\u5c5e\u6027\uff0c\u540c\u65f6\u7ed9\u5b83\u52a0\u4e0a\u4e00\u4e2a`resize`\u7684\u5c5e\u6027\uff08\u53ef\u4ee5\u914d\u7f6e\u7684\u6709`contain/cover/stretch`\uff09
+      
+  ```
+  @interface WXImageComponent ()
+  
+  @property (nonatomic, strong) NSString *imageSrc;
+  @property (nonatomic, assign) UIViewContentMode resizeMode;
+  
+  @end
+  ```
+   component\u4e2d\u6240\u6709\u7684style\uff0cattribute\uff0cevents\u90fd\u4f1a\u88ab\u4f20\u9012\u5230 Component\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\uff0c\u6240\u4ee5\uff0c\u4f60\u53ef\u4ee5\u5728\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u5b58\u50a8\u4f60\u611f\u5174\u8da3\u7684\u4e00\u4e9b\u5c5e\u6027\u503c
+      
+  ```
+  @implementation WXImageComponent
+  
+  - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
+  {
+      if (self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance]) {
+          _imageSrc = [WXConvert NSString:attributes[@"src"]];
+          _resizeMode = [WXConvert UIViewContentMode:attributes[@"resize"]];
+  }
+  
+      return self;
+  }
+  
+  @end
+  ```
+      
+   attribute\u4e2d\u62ff\u5230\u7684\u503c\u7684\u7c7b\u578b\u90fd\u662f`id`,\u6211\u4eec\u53ef\u4ee5\u7528\u8f6c\u6362\u65b9\u6cd5\u628a\u5b83\u8f6c\u6362\u5230\u4efb\u4f55\u503c\u3002Weex SDK\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u7840\u7684\u8f6c\u6362\u65b9\u6cd5\uff0c\u53ef\u4ee5\u53c2\u8003 `WXConvert`\u7c7b\uff0c\u6216\u8005\u4f60\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u8f6c\u6362\u51fd\u6570
+   
+   1. Hooking \u6e32\u67d3\u751f\u547d\u5468\u671f  
+         native \u7684component \u662f\u7531Weex\u7ba1\u7406\u7684\uff0cweex \u521b\u5efa\uff0c\u5e03\u5c40\uff0c\u6e32\u67d3\uff0c\u9500\u6bc1\u3002weex\u7684component\u751f\u547d\u5468\u671f\u90fd\u662f\u53ef\u4ee5hook\u7684\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e9b\u751f\u547d\u5468\u671f\u4e2d\u53bb\u505a\u81ea\u5df1\u7684\u4e8b\u60c5
+      
+  | \u65b9\u6cd5 | \u63cf\u8ff0 |
+  | :-: | --- |
+  | initWithRef:type:... | \u7528\u7ed9\u5b9a\u7684\u5c5e\u6027\u521d\u59cb\u5316\u4e00\u4e2acomponent. |
+  | layoutDidFinish | \u5728component\u5b8c\u6210\u5e03\u5c40\u65f6\u5019\u4f1a\u8c03\u7528. |
+  | loadView | \u521b\u5efacomponent\u7ba1\u7406\u7684view. |
+  | viewWillLoad | \u5728component\u7684view\u52a0\u8f7d\u4e4b\u524d\u4f1a\u8c03\u7528. |
+  | viewDidLoad | \u5728component\u7684view\u52a0\u8f7d\u5b8c\u4e4b\u540e\u8c03\u7528. |
+  | viewWillUnload | \u5728component\u7684view\u88ab\u91ca\u653e\u4e4b\u524d\u8c03\u7528. |
+  | viewDidUnload | \u5728component\u7684view\u88ab\u91ca\u653e\u4e4b\u540e\u8c03\u7528. |
+  | updateStyles: | \u5728component\u7684style\u66f4\u65b0\u65f6\u5019\u8c03\u7528. |
+  | updateAttributes: | \u5728component\u7684attribute\u66f4\u65b0\u65f6\u5019\u8c03\u7528. |
+  | addEvent: | \u7ed9component\u6dfb\u52a0event\u7684\u65f6\u5019\u8c03\u7528. |
+  | removeEvent: | \u5728event\u79fb\u9664\u7684\u65f6\u5019\u8c03\u7528. |
+      
+   \u5728image component\u7684\u4f8b\u5b50\u91cc\u9762\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u6211\u4eec\u81ea\u5df1\u7684image view \u7684\u8bdd\uff0c\u53ef\u4ee5\u590d\u5199 `loadView`\u8fd9\u4e2a\u65b9\u6cd5.
+   
+   ```
+   - (UIView *)loadView
+   {
+       return [[WXImageView alloc] init];
+   }
+   ```
+   
+   \u73b0\u5728\u6211\u4eec\u4f7f\u7528 `WXImageView` \u6e32\u67d3 `image` component\u3002  
+   1. \u4f5c\u4e3a\u4e00\u4e2aimage component\uff0c\u6211\u4eec\u9700\u8981\u62ff\u5230\u670d\u52a1\u5668\u56fe\u7247\uff0c\u800c\u4e14\u628a\u5b83\u8bbe\u7f6e\u8fdbimage view \u91cc. \u8fd9\u4e2a\u64cd\u4f5c\u53ef\u4ee5\u5728 `viewDidLoad` \u65b9\u6cd5\u4e2d\u505a\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728view\u5df2\u7ecf\u88ab\u521b\u5efa\u800c\u4e14\u52a0\u8f7d\u4e86\u65f6\u5019weex SDK\u4f1a\u8c03\u7528\u5230\uff0c\u800c\u4e14`viewDidLoad`\u8fd9\u4e2a\u65b9\u6cd5\u662f\u4f60\u505a\u989d\u5916\u521d\u59cb\u5316\u5de5\u4f5c\u6bd4\u5982\u6539\u53d8content mode(\u4e5f\u5c31\u662f\u8bbe\u7f6eresize) \u7684\u6700\u597d\u65f6\u95f4.
+   
+   ```
+   - (void)viewDidLoad
+   {
+       UIImageView *imageView = (UIImageView *)self.view;
+       imageView.contentMode = _resizeMode;
+       imageView.userInteractionEnabled = YES;
+       imageView.clipsToBounds = YES;
+       imageView.exclusiveTouch = YES;
+   
+       // Do your image fetching and updating logic
+   }
+   ```
+   
+ 1. \u5982\u679c\u53ef\u4ee5\u6539\u53d8image\u7684src,\u4e5f\u53ef\u4ee5hook `updateAttributes:`\u65b9\u6cd5\u6765\u505a\u5c5e\u6027\u66f4\u65b0\u64cd\u4f5c\uff0c\u5f53`updateAttributes:`\u6216\u8005 `updateStyles:`\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c component\u7684view \u5df2\u7ecf\u52a0\u8f7d\u5b8c\u6210
+   
+   ```
+   - (void)updateAttributes:(NSDictionary *)attributes
+   {
+       if (attributes[@"src"]) {
+           _imageSrc = [WXConvert NSString:attributes[@"src"]];
+           // Do your image updating logic
+       }
+   
+       if (attributes[@"resize"]) {
+           _resizeMode = [WXConvert UIViewContentMode:attributes[@"resize"]];
+           self.view.contentMode = _resizeMode;
+       }
+   }
+   ```
+   
+   \u6216\u8bb8\u4f60\u9700\u8981\u8003\u8651\u66f4\u591a\u7684\u751f\u547d\u5468\u671f\u65b9\u6cd5\u53bbHook\uff0c\u5f53\u5e03\u5c40\u5b8c\u6210\u65f6\u5019\uff0c\u50cf`layoutDidFinish`\uff0c\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\uff0c\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b`WXComponent.h` \u58f0\u660e\u7684\u65b9\u6cd5
+   \u73b0\u5728\u4f60\u53ef\u4ee5\u7528\u5728\u4efb\u4f55 .we\u6587\u4ef6\u91cc\u9762\u4f7f\u7528 `<image>`\uff0c\u800c\u4e14\u53ef\u4ee5\u52a0\u4e0a image\u7684\u5c5e\u6027
+   
+   ```
+   <image style="your-custom-style" src="image-remote-source" resize="contain/cover/stretch"></image>
+   ```
+##### component \u65b9\u6cd5
+  WeexSDK 0.9.5 \u4e4b\u540e\u652f\u6301\u4e86\u5728js\u4e2d\u76f4\u63a5\u8c03\u7528component\u7684\u65b9\u6cd5\uff0c\u8fd9\u91cc\u63d0\u4f9b\u4e00\u4e2a\u4f8b\u5b50\uff0c
+  
+  - \u81ea\u5b9a\u4e49\u4e00\u4e2aWXMyCompoenent \u7684\u7ec4\u4ef6
+  
+	 ```
+	 @implementation WXMyComponent
+	 	WX_EXPORT_METHOD(@selector(focus)) // \u66b4\u9732\u8be5\u65b9\u6cd5\u7ed9js
+	 - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
+	 {
+	     if (self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance]) {
+	         // handle your attributes
+	         // handle your styles
+	     }
+	     
+	     return self;
+	 }
+	 
+	 - (void)focus
+	   {
+	   		NSLog(@"you got it");
+	   }
+	 @end
+	 ```
+	
+	- \u6ce8\u518c\u7ec4\u4ef6 `[WXSDKEngine registerComponent:@"mycomponent" withClass:[WXMyComponent class]] `
+	- \u5728weex \u6587\u4ef6\u4e2d\u8c03\u7528
+
+		```
+		<template>
+	     		<mycomponent id='mycomponent'></mycomponent>
+	 	</template>
+		<script>
+		   module.exports = {
+		    	created: function() {
+		    		this.$el('mycomponent').focus();
+		    		}
+		   }
+		</script>
+ 		``` 
+ 
+ 
+ 
+ 
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/9b69a04b/doc/source/v-0.10/advanced/extend-to-android.md
----------------------------------------------------------------------
diff --git a/doc/source/v-0.10/advanced/extend-to-android.md b/doc/source/v-0.10/advanced/extend-to-android.md
index dec6758..4816578 100644
--- a/doc/source/v-0.10/advanced/extend-to-android.md
+++ b/doc/source/v-0.10/advanced/extend-to-android.md
@@ -31,7 +31,7 @@ public class WXEventModule extends WXModule{
 
   private static final String WEEX_CATEGORY="com.taobao.android.intent.category.WEEX";
 
-    @WXModuleAnno
+    @JSMethod
     public void openURL(String url){
       //implement your module logic here
     }
@@ -58,7 +58,7 @@ event.openURL("http://www.github.com");
 If the module need implement a callback to javascript, you just add `JSCallback` argument to the method you want expose to javascript:
 
 ```java
-  @WXModuleAnno
+  @JSMethod
   public void openURL(String url,JSCallback callback){
     //implement your module logic here
     Map<String,Object> resp = new HashMap();
@@ -95,19 +95,19 @@ public class MyViewComponent extends WXComponent{
   public MyViewComponent(WXSDKInstance instance, WXDomObject dom,
                      WXVContainer parent, String instanceId, boolean isLazy)
    {
-   public MyViewComponent(WXSDKInstance instance, WXDomObject dom,
-     WXVContainer parent, String instanceId, boolean isLazy) {
-    super(instance, dom, parent, instanceId, isLazy);
-   }
-
-   @Override
-   protected void initView() {
-      mHost = new TextView(mContext);
-   }
-   @WXComponentProp(name=WXDomPropConstant.WX_ATTR_VALUE)
-   public void setMyViewValue(String value) {
-      ((TextView)mHost).setText(value);
-   }
+	   public MyViewComponent(WXSDKInstance instance, WXDomObject dom,
+	     WXVContainer parent, String instanceId, boolean isLazy) {
+	    super(instance, dom, parent, instanceId, isLazy);
+	   }
+
+	   @Override
+	   protected void initView() {
+	      mHost = new TextView(mContext);
+	   }
+	   @WXComponentProp(name=WXDomPropConstant.WX_ATTR_VALUE)
+	   public void setMyViewValue(String value) {
+	      ((TextView)mHost).setText(value);
+	   }
 }
 ```
 
@@ -160,3 +160,30 @@ public class ImageAdapter implements IWXImgLoaderAdapter {
   }
 }
 ```
+#### Component Method
+ from WeexSDK `0.9.5`, you can define your component method
+
+ for example, define a method in component:
+ 
+ ```java
+ 
+ @JSMethod
+ public void focus(){
+ 	//method implementation
+ }
+ 
+ ```
+ after your registration for your own custom component, now you can call it in your js file.
+ 
+ ```html
+<template>
+ 		<mycomponent id='mycomponent'></mycomponent>
+</template>
+<script>
+   module.exports = {
+    	created: function() {
+    		this.$el('mycomponent').focus();
+    		}
+   }
+</script>
+``` 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/9b69a04b/doc/source/v-0.10/advanced/extend-to-ios.md
----------------------------------------------------------------------
diff --git a/doc/source/v-0.10/advanced/extend-to-ios.md b/doc/source/v-0.10/advanced/extend-to-ios.md
index 1ef0a38..cfc69cd 100644
--- a/doc/source/v-0.10/advanced/extend-to-ios.md
+++ b/doc/source/v-0.10/advanced/extend-to-ios.md
@@ -261,6 +261,45 @@ Now you can use `<image>` and its attributes wherever you want in the template.
 ```html
 <image style="your-custom-style" src="image-remote-source" resize="contain/cover/stretch"></image>
 ```
+#### Component Method
+from WeexSDK `0.9.5`, you can define your component method by macro `WX_EXPORT_METHOD`
+for example:
+
+```
+@implementation WXMyComponent
+ +WX_EXPORT_METHOD(@selector(focus))
+ +- (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
+ {
+     if (self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance]) {
+         // handle your attributes
+         // handle your styles
+     }
+     
+     return self;
+ }
+
+ 
+ - (void)focus
+   {
+   		NSLog(@"you got it");
+   }
+@end
+```
+   
+ after your registration for your own custom component, now you can call it in your js file.
+ 
+```
+<template>
+ 		<mycomponent id='mycomponent'></mycomponent>
+</template>
+<script>
+   module.exports = {
+    	created: function() {
+    		this.$el('mycomponent').focus();
+    		}
+   }
+</script>
+```