Giter Site home page Giter Site logo

backbone-server-rendering-example's Introduction

Backbone rendering on server and client.

The example in this repo is modeled after this one using React to render on both client and server. Setting aside the client-side benefits of React, I wanted to draw a comparison and explore the differences.

I'm open to questions and comments about this. I created a space here. #1

Usage

Just npm install and then npm start. Then open your browser to http://localhost:4000/

Analysis

The React example is pretty straight forward. The Backbone example was much less so. Backbone is coupled to the DOM in some pretty glaring ways. So getting around that, even for a simple example, requires some hacks.

Server-side rendering

React comes with built-in support for rendering a Component as a string. I worked up an ad-hoc mixin to do this with Backbone Views. Consequently that's where most of the hacks reside. I didn't look for an existing module that may accomplish this in a more robust way. But to be fair, once the complexity is encapsulated here, doing the server-side rendering is just as straight forward with Backbone.

Client-side js delivery

We're using browserify to bundle the client-side js. I had a hell of a time getting Backbone to find jQuery and use it. There may be a better way to accomplish this. React includes all dependecies so this was a no-issue.

DOM dependency

Backbone depends on jQuery or a jQuery-like library. On the node side, I have to patch Backbone.$. It's called on initialization, even though it's not strictly needed.

Backbone likes to have a top level DOM element, and the render() method just fills in the inner content. This makes rendering the full template server-side less than straight-forward. The method I'm using is hacky. And it doesn't support attributes on the top level element. This is the biggest source of incompatibility IMO.

Dynamic client-side updates

The scope of the example adds a client-side interval that updates data for some interactivity. On the node side, letting the interval run will cause re-renders which can cause errors. React side-steps this problem due to the lifecycle support. The interval tick is only called when DOM is being used. I haven't dug into the details of Reacts lifecycle, but it is handy here. I had to hack browser environment detection for Backbone. Backbone really needs lifecycle events.

Another issue in the Backbone example is that the input field is constantly re-rendered as part of the Timer view. So it constantly loses focus and you can't type into it. This could be fixed by moving the input markup outside of the view. But avoiding full re-renders is not be an unreasonable requirement in real code. Partial update methods like React (and Ember I assume) have a clear advantage here.

backbone-server-rendering-example's People

Contributors

polotek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

backbone-server-rendering-example's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.