MVC Architecture. Routing

Slides:



Advertisements
Similar presentations
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
Advertisements

AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Software Technologies
Version Control Systems
Magento Basics Getting started developing for Magento
Auto Mapping Objects SoftUni Team Database Applications
Databases basics Course Introduction SoftUni Team Databases basics
C# MVC Frameworks – ASP.NET
ASP.NET Essentials SoftUni Team ASP.NET MVC Introduction
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
WordPress Introduction
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
JavaScript Applications
Application Architecture, Redux
ASP.NET Integration Testing
Mocking tools for easier unit testing
JavaScript Applications
State Management Cookies, Sessions SoftUni Team State Management
Browser Routing Design Patterns in JS
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
Software Technologies
Entity Framework: Code First
Unit Testing with Mocha
Databases advanced Course Introduction SoftUni Team Databases advanced
C#/Java Web Development Basics
Creating React Components with JSX and Babel
Install and configure theme
Entity Framework: Relations
Caching Data in ASP.NET MVC
The Right Way Control Flow
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
ASP.NET MVC Introduction
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best Practices and Architecture
C# Web Development Basics
Creating UI elements with Handlebars
Best practices and architecture
Design & Module Development
Magento Basics part 2 Modules & Themes Stenik Group Ltd. Magento
Web Fundamentals (HTML and CSS)
WordPress Plugins Popular WP Plugins: Sliders, Forms, Contacts, SEO, Forum, Photo Gallery, e-Commerce WordPress Plugins SoftUni Team Technical Trainers.
Extending functionality using Collections
ASP.NET REST Services SoftUni Team ASP.NET REST Services
ASP.NET Filters SoftUni Team ASP.NET MVC Introduction
Making big SPA applications
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Course Overview, Trainers, Evaluation
JavaScript Fundamentals
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Directives & Forms Capturing User Input SoftUni Team
Version Control Systems
JavaScript Frameworks & AngularJS
Lean .NET stack for building modern web apps
JavaScript: ExpressJS Overview
CSS Transitions and Animations
Iterators and Generators
Presentation transcript:

MVC Architecture. Routing Model-View-Controller. Using React Router in JS SPA MVC + Routing SoftUni Team Technical Trainers Software University http://softuni.bg

Table of Contents MVC in JavaScript Web Routing ReactJS Models Views Controllers Web Routing ReactJS

Have a Question? sli.do #3243

MVC Architecture Overview Terminology Models – describe data and rules for handling data Views – represent parts of the data to the user Controllers – parse user input to views and models

MVC Example Controller Model View Database User Supervises the data transfer and communication between the Model and the View… Model View Communication with the Database… User interaction, Data visualization... Database User

Model Entity Model Represents the data stored in a DB Represented in memory You can create, edit and delete an entity You can filter and sort entities by specific criteria

View Entity details page Displays only a section of the data the Model served The same data can be shown by different Views Style can be altered independent of the data

Controller Edit entity Reads and parses information the User submitted Instructs the View to update accordingly Sends parsed information to the Model

MVC in JavaScript with React React Controller Views and stateless Views Lift shared data to common ancestor Split components in smaller pieces Provide a central source of truth and parsing Models – data management and storage inside Controller View or in separate module

Web Routing index.html Catalog New Products About Discount Contact

Problem Solution Web Routing Overview Changing the URL reloads the page Solution Single Page Application with JavaScript

Web Routing Overview (2) New Problem We can't track browser history Back button won't work

Web Routing Overview (2) Solution Reflect content changes in the URL Track URL changes and load content

A React add-on for web routing React Router A React add-on for web routing

What is React Router? React Router is a complete routing library for React Keeps the UI in sync with the URL ReactDOM.render(( <Router history={hashHistory}> <App> <Route path="/catalog" component={Catalog}/> <Route path="/about" component={About}/> </App> </Router> ), document.getElementById('root'))

Installation and Setup Install using npm from the terminal Import modules in your app npm install --save react-router import { Router, browserHistory } from 'react-router' import { Route, IndexRoute, Link } from 'react-router'

Rendering a Route React Router is a component It can be rendered as any React component React components can be nested in it as normal render(<Router/>, document.getElementById('app')) render(( <Router history={hashHistory}> <App /> </Router> ), document.getElementById('app'))

Adding more scenes React components can be wrapped in a Route and bound to a specific path Access URL <Router history={hashHistory}> <Route path="/" component={App}/> <Route path="/catalog" component={Catalog}/> <Route path="/about" component={About}/> </Router> Component to load

Access URL as defined in Route Navigating with Link Link replaces <a> and automatically prevents page reload let App = React.createClass({ render() { return ( <div><h1>React Router Tutorial</h1> <Link to="/catalog">Catalog</Link> <Link to="/about">About</Link> </div> ) }}) Access URL as defined in Route

Nested Routes With props.children we can dynamically nest components let App = React.createClass({ render() { return ( <div><h1>React Router Tutorial</h1> <Link to="/catalog">Catalog</Link> <Link to="/about">About</Link> {this.props.children} </div> ) }})

Nested routes will appear as children inside App Our navigation links will now appear on all pages Nested routes will appear as children inside App ReactDOM.render(( <Router history={hashHistory}> <Route path="/" component={App}> <Route path="catalog" component={Catalog}/> <Route path="about" component={About}/> </Route > </Router> ), document.getElementById('root'))

Active Links Links know when they are currently active We can style them with activeStyle or activeClassName <Link to="/catalog" activeStyle={{ color: 'red' }}> Catalog </Link> <Link to="/catalog" activeClassName="activeNav"> Catalog </Link>

Will display as children when we open "/" Index Routes We can specify a default component when no route is active Will display as children when we open "/" <Router history={hashHistory}> <Route path="/" component={App}> <IndexRoute component={Home}/> <Route path="catalog" component={Catalog}/> <Route path="about" component={About}/> </Route > </Router>

Clean URLs with Browser History Configure the Router to work with browserHistory This removes the pound sign from our links (#) import { browserHistory } from 'react-router' render(( <Router history={browserHistory}> … </Router> ), document.getElementById('app'))

This part of the URL can change URL Params Parameters are dynamic parts of the URL Example: /catalog/elecronics/XYZ5538 Configure the Route to work with params Access from the component This part of the URL can change <Route path="/catalog/:category/:userId" component={Catalog}/> this.props.params.category

Practice: Routing with React Router Live Exercises in Class (Lab)

Summary MVC makes large projects easier to manage With React Router we can compose a large app from smaller pieces <Router history={hashHistory}> <Route path="/" component={App}> <Route path="catalog" component={Catalog}/> <Route path="about" component={About}/> </Route > </Router>

MVC Architecture. Routing https://softuni.bg/courses/javascript-applications © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.