2019-07-15 19:31:17 +08:00
|
|
|
|
define('docs/sdk.md', function(require, exports, module) {
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
"title": "如何定制",
|
2019-07-16 11:46:50 +08:00
|
|
|
|
"html": "<p>开始定制之前,请先仔细阅读工作原理。</p>\n<h2><a class=\"anchor\" name=\"%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86\" href=\"#%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86\" aria-hidden=\"true\"><svg aria-hidden=\"true\" class=\"octicon octicon-link\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>工作原理</h2><p>amis 的渲染过程就是将 <code>json</code> 转成对应的 React 组件。先通过 <code>json</code> 的 type 找到对应的 <code>Component</code> 然后,然后把其他属性作为 <code>props</code> 传递过去完成渲染。</p>\n<p>拿一个表单页面来说,如果用React组件调用大概是这样。</p>\n<pre><code class=\"lang-jsx\"><<span class=\"hljs-meta\">Page</span>\n <span class=\"hljs-meta\">title</span>=<span class=\"hljs-string\">\"页面标题\"</span>\n subTitle=<span class=\"hljs-string\">\"副标题\"</span>\n>\n <Form\n <span class=\"hljs-meta\">title</span>=<span class=\"hljs-string\">\"用户登录\"</span>\n controls={[\n {\n type: <span class=\"hljs-string\">'text'</span>,\n name: <span class=\"hljs-string\">'username'</span>,\n <span class=\"hljs-meta\">label</span>: <span class=\"hljs-string\">'用户名'</span>\n }\n ]}\n />\n</<span class=\"hljs-meta\">Page</span>>\n</code></pre>\n<p>把以上配置方式换成 amis JSON, 则是:</p>\n<pre><code class=\"lang-json\">{\n <span class=\"hljs-attr\">\"type\"</span>: <span class=\"hljs-string\">\"page\"</span>,\n <span class=\"hljs-attr\">\"title\"</span>: <span class=\"hljs-string\">\"页面标题\"</span>,\n <span class=\"hljs-attr\">\"subTitle\"</span>: <span class=\"hljs-string\">\"副标题\"</span>,\n <span class=\"hljs-attr\">\"body\"</span>: {\n <span class=\"hljs-attr\">\"type\"</span>: <span class=\"hljs-string\">\"form\"</span>,\n <span class=\"hljs-attr\">\"title\"</span>: <span class=\"hljs-string\">\"用户登录\"</span>,\n <span class=\"hljs-attr\">\"controls\"</span>: [\n {\n <span class=\"hljs-attr\">\"type\"</span>: <span class=\"hljs-string\">\"text\"</span>,\n <span class=\"hljs-attr\">\"name\"</span>: <span class=\"hljs-string\">\"username\"</span>,\n <span class=\"hljs-attr\">\"label\"</span>: <span class=\"hljs-string\">\"用户名\"</span>\n }\n ]\n }\n}\n</code></pre>\n<p>那么,amis 是如何将 JSON 转成组件的呢?直接根据节点的 type 去跟组件一一对应?似乎很可能会重名比如在表格里面展示的类型 <code>text</code> 跟表单里面的 <code>text</code>是完全不一样的,一个负责展示,一个却负责输入。所以说一个节点要被什么组件渲染,还需要携带上下文(context)信息。</p>\n<p>如何去携带上下文(context)信息?amis 中直接是用节点的路径(path)来作为上下文信息。从上面的例子来看,一共有三个节点,path 信息分别是。</p>\n<ul>\n<li><code>page</code> 页面节点</li>\n<li><code>page/body/form</code> 表单节点</li>\n<li><code>page/body/form/controls/0/text</code> 文本框节点。</li>\n</ul>\n<p>根据 path 的信息就能很容易注册组件跟节点对应了。</p>\n<p>Page 组件的示例代码</p>\n<pre><code class=\"lang-jsx\"><span class=\"hljs-meta\">@Renderer</span>({\n test: /^page$/,\n <span class=\"hljs-comment\">// ... 其他信息隐藏了</span>\n})\nexport <span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">PageRenderer</span> <span class=\"hljs-keyword\">extends</span> <span class=\"hljs-title\">React</span>.<span class=\"hljs-title\">Compon
|
2019-07-15 19:31:17 +08:00
|
|
|
|
"toc": {
|
|
|
|
|
"label": "目录",
|
|
|
|
|
"type": "toc",
|
|
|
|
|
"children": [
|
|
|
|
|
{
|
|
|
|
|
"label": "工作原理",
|
|
|
|
|
"fragment": "%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86",
|
|
|
|
|
"fullPath": "#%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86",
|
|
|
|
|
"level": 2
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"label": "自定义组件",
|
|
|
|
|
"fragment": "%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6",
|
|
|
|
|
"fullPath": "#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6",
|
|
|
|
|
"level": 2
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"level": 0
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|