MVC way to introduce Sails.js - node.js framework

Post on 23-Aug-2014

3.050 views 1 download

Tags:

description

introduce Sails.js in MVC way, we all know MVC for web development, let looking for Sails.js for Node.js Web application MVC infrastructure and development.

Transcript of MVC way to introduce Sails.js - node.js framework

MVC way to introduce sails.js - Caesar Chi

http://sailsjs.org/#!Monday, May 12, 14

Web, MVC

Monday, May 12, 14

Monday, May 12, 14

Monday, May 12, 14

Rails like framework, but extends node.js featureMonday, May 12, 14

http://sailsjs.org/

Monday, May 12, 14

Sails.js•Auto Routing•Express base•Scaffolding•Socket.io support•Restful blueprint

Monday, May 12, 14

SupportandEnhance

Monday, May 12, 14

Monday, May 12, 14

Blueprint default support, you do not key in CRUD

https://github.com/hankejh/blueprintMonday, May 12, 14

Model module, use Waterlinehttps://github.com/balderdashy/waterline

Monday, May 12, 14

Route Setting is

*ucking EASY

Monday, May 12, 14

Scaffoldingcreate

controllers models

in 1 secondMonday, May 12, 14

Blueprint built-inRestful service in

1 second

Monday, May 12, 14

How to start?

Monday, May 12, 14

npm install -g sails

cli

Monday, May 12, 14

Quick start

Monday, May 12, 14

run app

• cd project

• node app

• sails lift

Monday, May 12, 14

Sails.js command line

Monday, May 12, 14

Sails generate ControllerModel

action ...

cli

Monday, May 12, 14

Generate

sails generate controllermodel name

blueprints include

cli

Monday, May 12, 14

Generate(cli)• Model

• path:

• /api/model

• Controller

• Path:

• /api/controller

Monday, May 12, 14

Generate(cli)• Model

• path:

• /api/model

• Controller

• Path:

• /api/controllerModel, Controller will be global object

Monday, May 12, 14

Controllerhttp://sailsjs.org/#!documentation/controllers

Monday, May 12, 14

sails generate controller Comment create destroy tag like

cli

Monday, May 12, 14

sails generate controller Comment create destroy tag like

cli

Request, Response

Monday, May 12, 14

CommentController.js

Monday, May 12, 14

CommentController.js

This is Model object

Monday, May 12, 14

Controller

• /api/controllers/

• http://sailsjs.org/#!documentation/controllers

Monday, May 12, 14

View

Monday, May 12, 14

View

• View -> as a render function

• Path:

• /view/...

• Support

• ejs (default)

• jade

Monday, May 12, 14

View

• View -> as a render function

• Path:

• /view/...

• Support

• ejs (default)

• jade

ejs (default)

Monday, May 12, 14

http://embeddedjs.com/

Monday, May 12, 14

Monday, May 12, 14

html layout

html body (content)

Monday, May 12, 14

view path

response.view()controller

Monday, May 12, 14

parameters

response.view()controller

Monday, May 12, 14

View Partials

Monday, May 12, 14

Response view

• res.view() = res.render();

• res.send({ some: 'json' });

• res.json({ user: 'tj' });

• res.redirect('/', 301);

Monday, May 12, 14

Routehttp://sailsjs.org/#!documentation/routes

Monday, May 12, 14

Route• Route

• Path

• /api/routes.js

Monday, May 12, 14

Route (default)default follow blueprint

URL route

Monday, May 12, 14

Route (default)default follow blueprint

Controller name

Controller action (function)

Monday, May 12, 14

view point to route

URL route View route

Monday, May 12, 14

Route(default)

Monday, May 12, 14

Route and actionconfig/Route.js

Monday, May 12, 14

Model

Monday, May 12, 14

Model

• file path -> api/models

• Capital naming

• Set schema and rule.

• ORM like

• you can use multi-adapter

Monday, May 12, 14

example, api/models/Post.js

Monday, May 12, 14

example, api/models/Post.js

defined key &

attribute

Monday, May 12, 14

example, controller call Post model

Monday, May 12, 14

example, controller call Post model

model name

Monday, May 12, 14

example, controller call Post model

model behavior

Monday, May 12, 14

example, controller call Post model

new data

Monday, May 12, 14

example, controller call Post model

success callback

& response

Monday, May 12, 14

Model documenthttp://sailsjs.org/#!documentation/models

Monday, May 12, 14

Controller, Model, Service, will be Global objectMonday, May 12, 14

MVC way examplehttps://github.com/nodejs-tw/example-week3

Monday, May 12, 14

@clonncd

Monday, May 12, 14

Sails.js

• sails.js version is 0.9.x

• the model part and service, we will talk in another chapter.

• keep coding, be passions.

Monday, May 12, 14