NinjiaJs
Micro and elegant frontend framework
Geting Started
Install
$ npm install ninjiajs
Pay A Glance
$ npm run dev
Usage
Startup
main.js
let app = // container, reducer, middlewares, initialState app appstartasync { // set entry for the application. app}
API
set(key, val)
-
key
<String> -
val
<Any>buildin config - key -> value
env
<Enum> application environment - production, development, testmode
<Enum> browser or historycontext
<Object> application context obj (store, tags, ...others)routes
<Object> expect a plain obj to describe the routes (more in below)entry
<Object> application entry component
registerWidget(options)
-
options.name
<string> -
options.methods
<array>allow user to control component with method invocation.
eg:
app.registerWidget({ name: 'modal', methods: ['open'] })
start()
all ready callback
Component
Define component
@Componnet // register this component to hub, in order to use it in html directly.Component { return ; } {}
State Management
redux like:
@Component {}
Router
Define routes
; component: App path: '' children: path: '/' component: TodoList defaultRoute: true
Fields
field | type | desc |
---|---|---|
path | string | Corresponding URI, Relative path |
component | Object | Component constructors |
children | Array | sub routes outlets |
defaultRoute | Boolean | specify that this is a default route |
components | Object | dynamic route defined, get identifier from query string |
abstract | Boolean | specify that this is a abstract route, no Corresponding component |
...others | Any | will be get from context.req.body |
Context
- req <Object>
field | type |
---|---|
params | Object |
body | Object |
query | Object |
Router outlet in html
greeting! <!-- sub routers will replace here -->
Component life cycle about router
evts |
---|
enter |
before-leave |
leave |
leaved |
eg:
@View // this decorator specify that component will be a view, give the component relevant featuresComponent // ...others { this this this this }
// Advanced UsageComponent // ...others // decorator onUse <Function> // @param <Array | String>, when nav to this component, the middlewares (defined in 'opts') will be invoke. // each middleware method will be injected a callback ( component will be present when the callback invoked ) and a // router context object. // eg: const enterFoo = (next, ctx) @ {}
Route hooks
1. history-pending - callback(from, to, location, context[, next])
from
<Object> from which Componentto
<Object> to which Componentlocation
<String> uricontext
<Object> context objectreq
<Object> request objectparams
<Object>body
<Object>query
<Object>
next
<Function> execution callback, if exists, the execution won`t be continue until next being executed
2. history-resolve - callback(from, to, context, routes, index[, next])
from
<Object> from which Componentto
<Object> to which Componentcontext
<Object> context objectreq
<Object> request objectparams
<Object>body
<Object>query
<Object>
routes
<Object> urisindex
<Number> current index in urisnext
<Function> execution callback, if exists, the execution won`t be continue until next being executed
eg:
app.hub.subscribe('history-pending', (from, to, location, context, next) => {})
app.hub.subscribe('history-resolve', (from, to, context, routes, index) => {})
Form
Buildin rules
- required
- max
- min
- maxlength
- minlength
- pattern
API
- registerValidators(name, fn)
- name <string>
- fn <Function>
Register customer validators
Detail
-
Integrate with Redux A action will be dispatched when interact with inputs. You will get corrent value in state and opts. Attached to opts:
- submit <Function> submit specific form manually.
- forms <Object> forms map.
-
input fields & add class
- field
- $valid
- $invalid
- $dirty
- $pristine
- $error
- $originVal
- $val
- class
- f-valid
- f-invalid
- f-dirty
- f-pristine
- field
-
multi form validation supported
Example
@Component // ...others async { e; thisopts // submit specific form manually if thisoptsformsFormA$invalid return; }
username required!
Usage v2.* (Deprecated)
routes.js
component: 'app' children: path: '/' defaultRoute: true component: 'count' path: '/test' component: 'test' children: path: '/test2' component: 'test2'
//main.js; let app = container reducer middlewares initialState; // create ninjia application app; app; app; routerhubroutes = routes; // set routes app; appstartasync { //todo}
//component ; <app> <div>Hello World</div> <router-outlet></router-outlet> ; this this; // mixin router, if you wanna use middleware (the $use method) this</app>
Who's using
More
source for more detail
Contact
QQ: 2811786667