Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何创建一个兼容「微信小程序」的Web框架:WINV #31

Open
phodal opened this issue Sep 28, 2016 · 0 comments
Open

如何创建一个兼容「微信小程序」的Web框架:WINV #31

phodal opened this issue Sep 28, 2016 · 0 comments

Comments

@phodal
Copy link
Owner

phodal commented Sep 28, 2016

在「微信小程序」带领Web走向封闭之前,让我们创造一个Neo的种子。如果有可能的话,那么有一天,它终将成为Neo。

maxresdefault.jpg

从微信小程序开始内测时,很多人(也包括我)都在考虑这样的问题:「微信小程序」正在让Web走向封闭。我的第一反应是:创建一个兼容「微信小程序」的Web框架——它即可以在微信上运行,也可以在Web上,还有作为一个混合应用运行。

在微信web开发者工具里,它封装了足够多的细节。我们只需要写一些我们不知道它们是如何真正工作的代码,流量都这样被截胡了。虽然,我们无法改变这个即将发生的事实,但是我们可以向那些愿意走向开放的人一个更好的解决方案。

因为「微信小程序」的框架是叫MINA,所以让我们称呼这个框架为WINV。

设计构思

基本的设计点有:

  • 兼容微信小程序的语法——它并没有多少复杂的语法。只是简单的Virtual DOM操作,以及事件绑定
  • 尽可能兼容大部分的微信API,兼容所有的微信API几乎是不可能的。
  • 提供一个Virtual DOM转换的混合应用插件。

在之前的文章里,我们提到了MINA框架的基本原理,也差不多就是组件:

  • WXML转JSON Virtual DOM组件
  • Virtual DOM组件,并在这其中提供双向绑定
  • UI组件转换器,即将WXML转换为Web浏览器中的标签
  • UI组件,需要有一套UI组件,最好是和小程序保持一致,如WEUI
  • AMD组件,提供模块化需求
  • APP引擎,需要有Page模块和APP模块,来处理页面逻辑,还有Route。

一个WINV框架的Demo

计划了好几天的Demo,终于写完了,并且可以出来溜溜了~~。

这份代码在GitHub上,欢迎试玩:https://github.com/phodal/winv

并创建一个更好的出来,毕竟国庆要和我们家 ‘花仲巴’出去玩。

好了,看我们的代码,这还只是一个丑陋的原型,但是差不多可以解释了。

var App = winv.App;
var Page = winv.Page;

App({
    onLaunch: function() {
        console.log('On Launch');
    }
});
Page({
    data: {
        motto: 'hello, world'
    },
    onLoad: function() {
        console.log('On Load');
    }
});

winv.setTemplate('<view class="container"><text class="user-motto">{{motto}}</text></view>')
winv.appRun();

它在页面上的运行结果就是,输出一个 hello, world。顺便吐槽一句,微信小程序自带的hello world不是标准的hello world。Wiki上说:

但是需要注意的是,Hello World的标准程序是“hello, world”,没有感叹号,全部小写,逗号后面有空格,与现在流行的写法并不一致。

出于原型的原因考虑,没有像MINA一样,使用大量的事件来触发,只是简单的Run:

var domJson = this.stringToDomJSON(this.template)[0];
var dom = this.jsonToDom(domJson);
document.getElementById('app').appendChild(dom);
for (var event in window.eventPool) {
  window.eventPool[event]();
}

第一步,将上面的Template转化为JSON格式的Template,由DOMParser将其转换为DOM,并在这个时候添加一个Page标签。然后在转换的时候,顺便做一些更新的数据操作。
第二步,这个JSON DOM在转换成真实的DOM的时候,应该要添加事件绑定,只是还没有实现。
第三步,上面的DOM会被放到app ID里,结果就变成了

<winv-div class="page">
    <winv-div class="page__hd">
        <winv-view class="container">
            <winv-text class="user-motto">hello, world</winv-text>
        </winv-view>
    </winv-div> 
</winv-div>

一看就知道还有好多坑要填。

第四步,则是调用上面的on方法,写得比较简单、粗暴。

至于,对事件和数据的判断还是和MINA一致:

if('on' === option.slice(0, 2))

简单,而又粗暴。

那么问题来了,有一天小程序真的封闭了,你会考虑来开始一个兼容的Web框架吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant