Teaching old java script new tricks

Preview:

Citation preview

Teaching Old JavaScript New Tricks

Experiences building a server-side JavaScript framework

Old JavaScript

• Over 15 years old

• Implemented everywhere:– Browsers, Web Servers, Scripting, Phones

• Server Implementations (Old):– Netscape, Rhino (JVM), Microsft IIS

• Old JavaScript feels “old”– Namespacing, polluted globals, poor inheritance,

not very modular..

• Those platforms don’t give you much

New and Modern Tricks

• Modules, Namespacing

• “Separation of Concerns”

• Model, View, Controller (MVC):

– Request Routing (Controller)

– Object Relational Mapping (Model)

– Templating (Views)

• Binary, UTF-8, JSON, Callbacks

• Concise and Elegant

Microsoft Javascript

• JScript on IIS (ASP)

• Not modern or cool, but old, stable and Windows hosting can be found everywhere

• JScript.dll updated with IE (up to 8) so it’s not ancient

• Not JScript.NET, not JIT, but quick enough (compared to other interpreters of it’s era)

Why JS on the Server

• Code re-use, sure..

• Real strength: Heaps of stuff already written and tested:

– Parsers, Crypto, ORM, Testing, etc.

– (I’ve even seen a Linux Emulator)

• Expressive and Powerful:

– Callbacks/handlers, Query Iteration, flexible inheritance, extendibility (override built-in methods)

• Poor dates, formatting, etc. But easily fixed.

The Framework

• Event Binding– Bind to the “ready” event to attach controller code / route

handlers

• Routes:– Sinatra-style; named parameters

• Modules: load modules similar to common-JS• Templating: compile your data in the route handler and

then use template module to render your view• Data Models: create your models, validation, methods

and relationships• Clean separation of code, helpful, intuitive interface

Into the Code

Hello World:

Named Parameters

• Access to URL params

• Automatic JSON Output

Using Libraries

• Similar to require() but uses lib()

Application Structure

• /app

– /config

– /controllers

– /models

– /system

– /views

• /assets

– Public files (css, images, etc)

Now the Cool Stuff

• ActiveRecord for JavaScript– Powerful modeling, no writing SQL

• HTML Parser, DOM implementation and jQuery on the Server– Read html files from file system and populate with dynamic

content– Webpage scraping with ease

• Node.js based dev-server– Uses windows scripting host behind Node to use framework

without

• Schema-less doc-store, Binary, Crypto, Advance language/full-text-search features, Persistent sessions with namespacing, etc.

ActiveRecord

• Models are defined inside app/models

• Ported from Aptana’s ActiveJS

Model Validation and Relationships

• hasOne, hasMany, belongsTo

• validatesPresenceOf, validatesUniquenessOf, validatesLengthOf, etc

• Custom Validation functions and getter Methods

Creating, Updating and Finding

• See Code

jQuery Server Side DOM

• See Code

Weaknesses and Future Development

• Needs better testing library (qunit)

• Debugging could be easier

• SQLite Adapter almost

• Working on adapters for other Server JS platforms (Apache + v8, Rhino on JVM)

• Good abstraction layers makes it not too closely coupled to current platform

Wrapping Up

• What can be learned from Do It Yourself

• Not Re-inventing the Wheel

• Re-using other open-source code

• Github: http://github.com/sstur/aspjs