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

60
MVC way to introduce sails.js - Caesar Chi http://sailsjs.org/#! Monday, May 12, 14

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

Page 1: MVC way to introduce Sails.js - node.js framework

MVC way to introduce sails.js - Caesar Chi

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

Page 2: MVC way to introduce Sails.js - node.js framework

Web, MVC

Monday, May 12, 14

Page 3: MVC way to introduce Sails.js - node.js framework

Monday, May 12, 14

Page 4: MVC way to introduce Sails.js - node.js framework

Monday, May 12, 14

Page 5: MVC way to introduce Sails.js - node.js framework

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

Page 6: MVC way to introduce Sails.js - node.js framework

http://sailsjs.org/

Monday, May 12, 14

Page 7: MVC way to introduce Sails.js - node.js framework

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

Monday, May 12, 14

Page 8: MVC way to introduce Sails.js - node.js framework

SupportandEnhance

Monday, May 12, 14

Page 9: MVC way to introduce Sails.js - node.js framework

Monday, May 12, 14

Page 10: MVC way to introduce Sails.js - node.js framework

Blueprint default support, you do not key in CRUD

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

Page 11: MVC way to introduce Sails.js - node.js framework

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

Monday, May 12, 14

Page 12: MVC way to introduce Sails.js - node.js framework

Route Setting is

*ucking EASY

Monday, May 12, 14

Page 13: MVC way to introduce Sails.js - node.js framework

Scaffoldingcreate

controllers models

in 1 secondMonday, May 12, 14

Page 14: MVC way to introduce Sails.js - node.js framework

Blueprint built-inRestful service in

1 second

Monday, May 12, 14

Page 15: MVC way to introduce Sails.js - node.js framework

How to start?

Monday, May 12, 14

Page 16: MVC way to introduce Sails.js - node.js framework

npm install -g sails

cli

Monday, May 12, 14

Page 17: MVC way to introduce Sails.js - node.js framework

Quick start

Monday, May 12, 14

Page 18: MVC way to introduce Sails.js - node.js framework

run app

• cd project

• node app

• sails lift

Monday, May 12, 14

Page 19: MVC way to introduce Sails.js - node.js framework

Sails.js command line

Monday, May 12, 14

Page 20: MVC way to introduce Sails.js - node.js framework

Sails generate ControllerModel

action ...

cli

Monday, May 12, 14

Page 21: MVC way to introduce Sails.js - node.js framework

Generate

sails generate controllermodel name

blueprints include

cli

Monday, May 12, 14

Page 22: MVC way to introduce Sails.js - node.js framework

Generate(cli)• Model

• path:

• /api/model

• Controller

• Path:

• /api/controller

Monday, May 12, 14

Page 23: MVC way to introduce Sails.js - node.js framework

Generate(cli)• Model

• path:

• /api/model

• Controller

• Path:

• /api/controllerModel, Controller will be global object

Monday, May 12, 14

Page 24: MVC way to introduce Sails.js - node.js framework

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

Monday, May 12, 14

Page 25: MVC way to introduce Sails.js - node.js framework

sails generate controller Comment create destroy tag like

cli

Monday, May 12, 14

Page 26: MVC way to introduce Sails.js - node.js framework

sails generate controller Comment create destroy tag like

cli

Request, Response

Monday, May 12, 14

Page 27: MVC way to introduce Sails.js - node.js framework

CommentController.js

Monday, May 12, 14

Page 28: MVC way to introduce Sails.js - node.js framework

CommentController.js

This is Model object

Monday, May 12, 14

Page 29: MVC way to introduce Sails.js - node.js framework

Controller

• /api/controllers/

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

Monday, May 12, 14

Page 30: MVC way to introduce Sails.js - node.js framework

View

Monday, May 12, 14

Page 31: MVC way to introduce Sails.js - node.js framework

View

• View -> as a render function

• Path:

• /view/...

• Support

• ejs (default)

• jade

Monday, May 12, 14

Page 32: MVC way to introduce Sails.js - node.js framework

View

• View -> as a render function

• Path:

• /view/...

• Support

• ejs (default)

• jade

ejs (default)

Monday, May 12, 14

Page 33: MVC way to introduce Sails.js - node.js framework

http://embeddedjs.com/

Monday, May 12, 14

Page 34: MVC way to introduce Sails.js - node.js framework

Monday, May 12, 14

Page 35: MVC way to introduce Sails.js - node.js framework

html layout

html body (content)

Monday, May 12, 14

Page 36: MVC way to introduce Sails.js - node.js framework

view path

response.view()controller

Monday, May 12, 14

Page 37: MVC way to introduce Sails.js - node.js framework

parameters

response.view()controller

Monday, May 12, 14

Page 38: MVC way to introduce Sails.js - node.js framework

View Partials

Monday, May 12, 14

Page 39: MVC way to introduce Sails.js - node.js framework

Response view

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

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

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

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

Monday, May 12, 14

Page 40: MVC way to introduce Sails.js - node.js framework

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

Monday, May 12, 14

Page 41: MVC way to introduce Sails.js - node.js framework

Route• Route

• Path

• /api/routes.js

Monday, May 12, 14

Page 42: MVC way to introduce Sails.js - node.js framework

Route (default)default follow blueprint

URL route

Monday, May 12, 14

Page 43: MVC way to introduce Sails.js - node.js framework

Route (default)default follow blueprint

Controller name

Controller action (function)

Monday, May 12, 14

Page 44: MVC way to introduce Sails.js - node.js framework

view point to route

URL route View route

Monday, May 12, 14

Page 45: MVC way to introduce Sails.js - node.js framework

Route(default)

Monday, May 12, 14

Page 46: MVC way to introduce Sails.js - node.js framework

Route and actionconfig/Route.js

Monday, May 12, 14

Page 47: MVC way to introduce Sails.js - node.js framework

Model

Monday, May 12, 14

Page 48: MVC way to introduce Sails.js - node.js framework

Model

• file path -> api/models

• Capital naming

• Set schema and rule.

• ORM like

• you can use multi-adapter

Monday, May 12, 14

Page 49: MVC way to introduce Sails.js - node.js framework

example, api/models/Post.js

Monday, May 12, 14

Page 50: MVC way to introduce Sails.js - node.js framework

example, api/models/Post.js

defined key &

attribute

Monday, May 12, 14

Page 51: MVC way to introduce Sails.js - node.js framework

example, controller call Post model

Monday, May 12, 14

Page 52: MVC way to introduce Sails.js - node.js framework

example, controller call Post model

model name

Monday, May 12, 14

Page 53: MVC way to introduce Sails.js - node.js framework

example, controller call Post model

model behavior

Monday, May 12, 14

Page 54: MVC way to introduce Sails.js - node.js framework

example, controller call Post model

new data

Monday, May 12, 14

Page 55: MVC way to introduce Sails.js - node.js framework

example, controller call Post model

success callback

& response

Monday, May 12, 14

Page 56: MVC way to introduce Sails.js - node.js framework

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

Monday, May 12, 14

Page 57: MVC way to introduce Sails.js - node.js framework

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

Page 58: MVC way to introduce Sails.js - node.js framework

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

Monday, May 12, 14

Page 59: MVC way to introduce Sails.js - node.js framework

@clonncd

Monday, May 12, 14

Page 60: MVC way to introduce Sails.js - node.js framework

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