Skip to content

LayoutManager in Node.js

Tim Branyen edited this page Oct 29, 2013 · 4 revisions

LayoutManager is fully compatible with Node.js. This means you can use the same API for rendering HTML on the server!

Installation

Please see the Installation section for details on how to install LayoutManager for use in Node.js.

Including

LayoutManager is included via Node.js's require API:

var LayoutManager = require("backbone.layoutmanager");

LayoutManager will use its own instance of Backbone.js unless you have installed Backbone.js prior to installing LayoutManager.

Usage

Besides the special considerations listed below, usage of LayoutManager in Node.js is identical to usage in the browser.

Special Considerations

Because LayoutManager code traditionally runs in the browser, there are a number of conventions that should be avoided when writing code for Node.js:

  • DOM: Simply put, there is no Document Object Model in Node.js. A view's el property may not function as it does in the browser. Additionally, DOM events like “click” or “focus” will neither bind nor trigger.o traverse and manipulate HTML structures.
  • jQuery: LayoutManager uses the cheerio library to render and traverse HTML structures. You can access Cheerio via LayoutManager.$, but please refer to the Cheerio documentation for details regarding API parity with jQuery.
  • Promises API: In Node.js, LayoutManager relies on the underscore.deferred library's implementation of when and Deferred.

Global Configuration

We've configured LayoutManager to work naturally when running in Node.js. This includes overrides to the following methods:

  • fetchTemplate reads from the filesystem
  • partial uses Cheerio

As always, you can override these methods as you please; see the documentation on Configuration for more details.

Thanks

Node.js support would not be possible without the help of these fine libraries: