项目作者: MithrilJS

项目描述 :
Use mithril views to render server side
高级语言: JavaScript
项目地址: git://github.com/MithrilJS/mithril-node-render.git
创建时间: 2014-10-15T08:45:03Z
项目社区:https://github.com/MithrilJS/mithril-node-render

开源协议:MIT License

下载


mithril-node-render

zulip, join chat
Build Status
rethink.js
no dependencies

Use mithril views to render server side

Demo

Usage Example

Installation

  1. npm install mithril-node-render

Usage

  1. // Make Mithril happy
  2. if (!global.window) {
  3. global.window = global.document = global.requestAnimationFrame = undefined
  4. }
  5. var m = require('mithril')
  6. var render = require('mithril-node-render')
  7. render(m('span', 'huhu')).then(function (html) {
  8. // html === '<span>huhu</span>'
  9. })
  10. var html = render.sync(m('span', 'huhu'))
  11. // html === '<span>huhu</span>'

Async components

As you see the rendering is asynchronous. It lets you await certain data from within oninit hooks.

  1. var myAsyncComponent = {
  2. oninit: function (node, waitFor) {
  3. waitFor(new Promise(function (resolve) {
  4. node.state.foo = 'bar'
  5. resolve()
  6. }))
  7. },
  8. view: function (node) {
  9. return m('div', node.state.foo)
  10. }
  11. }
  12. render(myAsyncComponent).then(function (html) {
  13. // html === '<div>bar</div>'
  14. })

Sync rendering

You can also render synchronously. You just don’t get the waitFor callback.

  1. var myAsyncComponent = {
  2. oninit: function (node, waitFor) {
  3. // waitFor === undefined
  4. new Promise(function (resolve) {
  5. node.state.foo = 'bar'
  6. resolve()
  7. })
  8. },
  9. view: function (node) {
  10. return m('div', node.state.foo)
  11. }
  12. }
  13. var html = render.sync(myAsyncComponent)
  14. // html === '<div>bar</div>'

Options

Optionally pass in options as an object: render(component, options).

The following options are supported:

escapeAttribute(value)
Default: render.escapeAttribute
A filter function for attribute values. Receives value, returns what is printed.

escapeText(value)
Default: render.escapeText
A filter function for string nodes. Receives value, returns what is printed.

strict
Default: false
Set this to true to close all empty tags automatically. Default is standard HTML mode where tags like <br> and <meta> are allowed to implicitly close themselves. This should be set to true if you’re rendering XML-compatible HTML documents.

xml
Default: false
Set this to true to render as generic XML instead of (possibly XML-compatible) HTML. Default is HTML mode, where children of void elements are ignored. This implies strict: true.

See also