18

Click here to load reader

Teaching old java script new tricks

Embed Size (px)

Citation preview

Page 1: Teaching old java script new tricks

Teaching Old JavaScript New Tricks

Experiences building a server-side JavaScript framework

Page 2: Teaching old java script new tricks

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

Page 3: Teaching old java script new tricks

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

Page 4: Teaching old java script new tricks

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)

Page 5: Teaching old java script new tricks

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.

Page 6: Teaching old java script new tricks

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

Page 7: Teaching old java script new tricks

Into the Code

Hello World:

Page 8: Teaching old java script new tricks

Named Parameters

• Access to URL params

• Automatic JSON Output

Page 9: Teaching old java script new tricks

Using Libraries

• Similar to require() but uses lib()

Page 10: Teaching old java script new tricks

Application Structure

• /app

– /config

– /controllers

– /models

– /system

– /views

• /assets

– Public files (css, images, etc)

Page 11: Teaching old java script new tricks

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.

Page 12: Teaching old java script new tricks

ActiveRecord

• Models are defined inside app/models

• Ported from Aptana’s ActiveJS

Page 13: Teaching old java script new tricks

Model Validation and Relationships

• hasOne, hasMany, belongsTo

• validatesPresenceOf, validatesUniquenessOf, validatesLengthOf, etc

• Custom Validation functions and getter Methods

Page 14: Teaching old java script new tricks

Creating, Updating and Finding

• See Code

Page 15: Teaching old java script new tricks

jQuery Server Side DOM

• See Code

Page 16: Teaching old java script new tricks

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

Page 17: Teaching old java script new tricks

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

Page 18: Teaching old java script new tricks