easiest
A minimal web mvvm framework.一个小Web mvvm库。 Now we support for typescript!
demo
npm run start
npm run start-js
- open
http://localhost:1234
Basic Usage
- Create a root DOM for route which id is root:
-
Create a Easiest:
-
use
$bootstrapModule
to bootstrap a root moduleEsModule
const easiest = ;easiest;easiest; // if u are using a routereasiest;
- Create a router:
-
routes must an
Array
includesObject
- routes must incloude rootPath
'/'
- routes must set an component name like
component: 'R1'
,R1
is declared inEsModule
in$declarations => this.$components
- routes can assign redirectTo and use
redirectTo: Path
- routes can assign children and use
children: Array
- routes can set a path like
path: '/:id'
- routes must incloude rootPath
-
if u are using
Router
, u must need torouter.$setRootPath('/RootPath')
to set an root path. -
router.$routeChange = (old, next)
can listen route change -
router.$init(routes);
can init Array routes -
if u want to watch routes changes, plz use
router.$routeChange=(old.new) => {}
- use
this.$setLocation((path: String, query: Object, params: Object)
to go to Path orlocation.href
- use
this.$getLocation()
to get location states Router
:http://localhost:1234/R1
- use
;;;router.$setRootPath'/demo'; // so routes:Array => `/` is `/demo`router.$initroutes;router.$routeChange =;;easiest.$bootstrapModuleM1;;easiest.$init;
- Create a Component:
-
create a
class
-
use decorator
Component
in typescript or use functionComponent
in javascript -
Component
accepts an objecttemplate: string;state?: any;
-
please use $setState or $setProps after lifecycle
constructor()
- typescript
- to use decorator
Component
declaretemplate
andstate
- to implements interface
HasRender, OnInit, WatchState, BeforeMount, AfterMount, RouteChange
to use lifecycle - to use decorator
Injectable
to injectService
inconstructor
's arguments ofComponent
- javascript
- to use function
Component
declaretemplate
andstate
- to use lifecycle
esOnInit esBeforeMount esAfterMount esOnDestory esHasRender esWatchState esRouteChange
in Class - to use
constructor
's arguments ofComponent
for injectService
, and arguments must be lowercase lette of initials lette of Service class name. For example, you want to inject a service classHeroService
, you must write argument inconstructor
withheroService
{thisss = heroSearchService;thisss;}{console;}{this;}{console;console;}Container; -
props: Object
is data whichclass Controller
sends toclass Component
-
**
props: Object
can only be changed or used after lifecycleconstructor()
** -
props: Object
can only be changed by actionthis.$setProps()
andthis.$setProps()
is equal to$setState
- EsModule
-
Easiest apps are modular and Easiest has its own modularity system called
EsModule
. AnEsModule
is a container for a cohesive block of code dedicated to an application domain, a workflow, or a closely related set of capabilities. It can contain components, service providers, and other code files whose scope is defined by the containingEsModule
. It can import functionality that is exported from otherEsModule
, and export selected functionality for use by otherEsModule
. -
u need to declare
imports?: Function[]
components: {[name: string]: Function;}
providers?: Function[]
exports?: string[]
bootstrap?: Function
inoptions
-
imports
imports otherEsModule
and respect it'sexports
-
components
declareComponents
. Key: name, Value: Components -
providers
declareService
-
exports:
exportsComponents
for otherEsModules
-
bootstrap
declareComponent
for Module bootstrap only if u don'tRouter
- typescript
- javascript
{}M1;
- Template Syntax
- 规定:指令以 es-xxx 命名
- now: es-text es-html es-model es-class es-repeat es-if es-on:Event
- 事件指令, 如 es-on:click
- Text1:
this.$template = '<p es-text="this.state.b"></p>';
- Text2:
this.$template = '<p>this.state.b是:{{this.state.b}}</p>';
- HTML:
this.$template = '<p es-html="this.state.c"></p>';
- Model for input:
this.$template = '<input es-model="this.state.c"/>';
if input is a repeat DOM, and intem of Array is'nt an object, please use$index
- Class:
this.$template = '<p class="b" es-class="this.state.a"></p>';
- Directives: ues
es-on:event
this.$template = '<p es-on:click="this.componentClick()"></p>';
- Repeat:
this.$template = '<p class="b" es-class="this.state.a" es-repeat="let a in this.state.b" es-if="a.f">{{a.z}}</p>';
- Text1:
- about function in Template Syntax
- now you can send arguments in Function
- arguments include:
- Event:
$event
- String:
'xxx'
- Number:
123
- Index:
$index
, you can only use this in repeat DOM :<input es-on:click="this.show(b, $index)" es-repeat="let b in this.state.testArray2" es-model="b" es-class="b" />
- Variable:
this.state.xxx
this.props.xxx
- Boolean:
true
false
- For es-repeat: items like:
es-repeat="let a in this.state.b" es-if="a.f"
- Event:
- Data monitor: this.state && this.$setState
- use
this.state: Object
andthis.$setState(parmars: Function || Object)
- if u have some variable, u can set
this.state
inconstructor(){}
- if u want to change State, plz use
this.$setState
, parmars can beObject
orFunction
which must return anObject
- and u can recive this change in life cycle
esWatchState(oldData, newData)
Watcher
andKeyWatcher
-
import {Watcher, KeyWatcher}
Watcher
- Watcher expects two arguments:
data, watcher
- data is an Object
- watcher is a function which has two arguments:
oldData, newData
thisobject {}
KeyWatcher
- Watcher expects there arguments:
data, key, watcher
- data:
Object
- key is a key in Object and type is
String
- watcher is a function which has two arguments:
oldData, newData
thisobject key {}
- Service
-
Components shouldn't fetch or save data directly and they certainly shouldn't knowingly present fake data. They should focus on presenting data and delegate data access to a service.
-
And u can use
Service
to send communication betweenComponent
, because we have realized singleton. -
Service
accepts an objectisSingletonMode: boolean
to decide use singleton or not.- typescript
- to use decorator
Injectable
to injectService
inconstructor
's arguments ofService
- javascript
- to use
constructor
's arguments ofService
for inject an otherService
, and arguments must be lowercase lette of initials lette of Service class name. For example, you want to inject a service classHeroSearchService
, you must write argument inconstructor
withheroSearchService
{thishsr = heroSearchService1;thishsr;}{console;}HeroSearchService;- http
;const http = esHttp;http;http;http;http;http;
-
Dependency Injection
Dependency injection is an important application design pattern. It's used so widely that almost everyone just calls it DI
- Use Typescript
- If u are using
Typescript
to build an app, u can easily use our Dependency Injection.Only use@Injectable
before theClass
which need to use other services, that which are declarated inthis.$providers
ofEsModule
or root module. - Use
this.
names of constructor arguments to directly useService
.
;- Use Javascript
- to use
constructor
's arguments ofService
for inject an otherService
, and arguments must be lowercase lette of initials lette of Service class name. For example, you want to inject a service classHeroSearchService
, you must write argument inconstructor
withheroSearchService
- A little diffrence between javascript and typescript, use constructor arguments to directly use
Service
, and assign them to a variable.
{super;}{console;}HeroSearchService1;{super;thishsr = heroSearchService1;thishsr;}{console;}HeroSearchService;{thisss = heroSearchService;thisss;}Container{}M1; -
LifeCycle hooks which from the beginning to the end:
-
EsModule
constructor -
Components
constructoresOnInit: void;esBeforeMount: void;esAfterMount: void;esOnDestory: void;esHasRender: void;esWatchStateoldData?: any, newData?: any: void;esRouteChangelastRoute?: string, newRoute?: string: void; -
Router
$routeChangelastRoute?: string, newRoute?: string: void;
Architecture
route => EsModule => component
To do
- 类分离,通过use来绑定方法
- 无需route渲染
- 子路由(2/2)
- 组件化(3/3)
- 模块化 + EsModule
- 双向绑定
- 公共类提取
- 数据劫持
- 双向绑定模板
- Template Syntax: es-text es-html es-model es-class es-repeat es-if(6/6)
- 组件props
- 组件渲染
- 组件中使用组件
- 改用 history的pushState
- 监听路由变化动态渲染(2/2)
- Virtual DOM
- Service
- Route bug
- ts (强类型赛高)
- DI