Templating, Routing, lodash Extending functionality using Collections SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
Advertisements

AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
PHP Basics Course Introduction SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Advanced JavaScript Course Introduction SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Redis Key-Value Database: Practical Introduction
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Database APIs and Wrappers
Entity Framework Performance SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
JavaScript Best Practices Learn How to Write Better Quality JavaScript Software University Technical Trainers SoftUni Team.
JavaScript Basics Course Introduction Svetlin Nakov Technical Trainer Software University
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
SASS & LESS Syntactically Awesome StyleSheets Dynamic StyleSheet Language Svetlin Nakov Technical Trainer Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
Associative Arrays and Objects Associative Arrays, Objects Svetlin Nakov Technical Trainer Software University
CSS Transitions and Animations Animated HTML Elements SoftUni Team Technical Trainers Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Advanced C# Course Introduction SoftUni Team Technical Trainers Software University
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Object-Oriented Programming Course Introduction Svetlin Nakov Technical Trainer Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Programming for Beginners Course Introduction SoftUni Team Technical Trainers Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
PHP Basics Course Introduction Svetlin Nakov Technical Trainer Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Software Technologies Course Overview SoftUni Team Technical Trainers Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Java OOP Advanced Course Introduction SoftUni Team Technical Trainers Software University
Extending functionality using Collections
CSS Transitions and Animations
Extending the Functionality of Collections
Presentation transcript:

Templating, Routing, lodash Extending functionality using Collections SoftUni Team Technical Trainers Software University

2  What is Sammy.js?  Create custom URLs  Using HTML templates with Mustache.js  Extend JavaScript functionality using lodash.js  What extensions does it offer?  Collection extensions  Array extensions  Function extensions  Object extensions Table of Contents

Application routing Defining URL structure of app

 JavaScript framework providing:  Routes – defining the basic structure of RESTful your application  Events – creating custom events implementing some logic  Well suited for building simple RESTful applications  Using JSON data stores What is Sammy.js?

5  Download sammy.js from  Include it in your project  Place sammy.js after jQuery  Create routes for your application Installing sammy.js

Application routing Live Demo

HTML Templating Fill HTML templates with data

 Logic-less templates:  Used for HTML, config files, source code – anything  Logic-less – does not contain if-else statements or loops.  Entirely defined by tags.  Runs on many languages like: Ruby, Python, JavaScript, PHP, Perl etc. What is Mustache.js?

9  Download mustache.js from  Include it in your project  Place mustache.js after jQuery  Create templates for your application Installing mustache.js

HTML Templating Live Demo

Lodash.js Extending JavaScript functionality

 Lodash.js is a JavaScript library, that extends regular JavaScript functionality  Provides extensions to object, arrays, selection, etc..  Usable is client JavaScript (web and mobile) and server JavaScript (Node.js) lodash.js

 Lodash extends the functionality for:  Collections  each, map, find, filter, where, some, countBy  Arrays  first, last, union, uniq, range  Functions  bind, delay, defer, once, after  Objects  keys, values, extend, functions, clone  Templates and Chaining lodash.js Functionality

Extensions for Collections

 Collections == arrays and objects  All underscore methods work both on arrays and objects (associative arrays)  Collection extensions:  _.each() - iterates over a collection  _.map(), _.pluck() - a projection of a collection  _.filter(), _.reject() and _.where - filter elements  _.all() and _.any() - evaluate a collection  _.sortBy(), _.groupBy() - sorts and groups Collections

 _.each() iterates over a list of elements, yielding each in turn to an iterator function  Similar to for-in loop  Uses the native forEach() function if the browser supports it Collections: each() var numbers = [1, 2, 3, 4, 5, 6, 7, 8]; _.each(numbers, console.log); _.each(numbers, function(item) { console.log(item); } //log all the numbers

_.each() Live Demo

 _.map() produces a new array of elements, after the values are computed  Uses the native map() function if the browser supports it  Can be used with objects as well: Collections: map() _.map(console, function (item) { return item.toString(); return item.toString();}); var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8]; var numberNames = ["zero", "one", "two", "three"]; function numbersToNames(item) { return numberNames[item]; } _.map(numbers, numbersToNames);

_.map() Live Demo

 Filter and reject return a subset of the original collection, based on an boolean expression  Filter returns all items matching the condition  Reject returns all items that do not fulfill the condition Collections: filter() and reject() var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8]; var even = _.filter(numbers, isEven); var odd = _.reject(numbers, isEven); function isEven(number) { return number % 2 === 0; return number % 2 === 0;}

_.filter() and _.reject() Live Demo

 _.where() filters a collection based on a property value  Returns a subcollection of the original collection Collections: where() var people = [{name: "Ivan Todorov", age: 21}, {name: "Todor Ivanov", age: 11}, {name: "Todor Ivanov", age: 11}, {name: "Petra Georgieva", age: 14}, {name: "Petra Georgieva", age: 14}, {name: "Georgi Petrov", age: 11}, {name: "Georgi Petrov", age: 11}, {name: "Stamina Staminova", age: 19}]; {name: "Stamina Staminova", age: 19}]; var elevenYearsOld = _.where(people, {age: 11}); // returns Todor Ivanov and Georgi Petrov

_.where() Live Demo

 _.all() returns true if ALL of the elements that meet a boolean expression  _.any() returns true if ANY of the elements fulfill a boolean condition  And false if none if the elements fulfill the condition Collections: all() and any() var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var anyEven = _.any(numbers, function(el){ return el % 2 == 0; }); //anyEven = true var allEven = _.all(numbers, function(el){ return el % 2 == 0; }); //allEven = false;

_.all() and _.any() Live Demo

 _.pluck() returns a projection of a collection  Returns only a part from all elements Collections: pluck() var people = [ {username: "VGeorgiev", {username: "VGeorgiev", {username : "Teodor92", {username : "Teodor92", {username : "nakov", {username : "nakov", {username : "Petya", {username : "Petya", var s = _.pluck(people, ' '); // s =

_.pluck() Live Demo

 _.sortBy() sorts the elements of a collection  Much like the native sort() function  Sorts by a property or an iterator function Collections: sortBy() var students = [ {fname: "Mitko", lname: "Ruletkata", age: 63}, {fname: "Mitko", lname: "Ruletkata", age: 63}, {fname: "Gencho", lname: "Ganev", age: 32}, {fname: "Gencho", lname: "Ganev", age: 32}, {fname: "Penio", lname: "Penev", age: 10}, {fname: "Penio", lname: "Penev", age: 10}, {fname: "Unufri", lname: "Unufriev", age: 17}]; {fname: "Unufri", lname: "Unufriev", age: 17}]; var sortedByLastname = _.sortBy(students, "lname"); var sortedByAge = _.sortBy(students, "age");

29  _.groupBy() groups the elements of a collection  Groups by a property or an iterator function Collections: groupBy() var cars = [ {make: "Nissan", model: "Skyline", color: "red"}, {make: "Audi", model: "A6", color: "blue"}, {make: "Lada", model: "Vesta", color: "red"}, {make: "Moskvich", model: "412", color: "yellow"}, {make: "Peugeot", model: "206", color: "red"}]; var carsByColor = _.groupBy(cars, "color");

_.sortBy() and _.groupBy() Live Demo

Array Extensions

 Array extensions work only on array objects  Does not work on associative arrays or objects  Array extensions:  _.first() and _.initial() selects the first n items  _.last() and _.rest() selects the last n items  _.compact() - removes all false values  _.union() and _.intersection() - unites or intersects two or more arrays Array Extensions

 _.first() returns the first element in an array  Can be used with a parameter to return the first n elements  _.initial() returns all elements except the last one  Can be used with a parameter to all the elements except the last n Arrays: first() and initial() var numbers = [1, 2, 3, 4, 5]; var initial = _.initial(numbers); // [1, 2, 3, 4] var initialTwo = _.initial(numbers, 2); // [1, 2, 3] var numbers = [1, 2, 3, 4, 5]; var first = _.first(numbers); // 1 var firstTwo = _.first(numbers, 2); // [1, 2]

_.first() and _.initial() Live Demo

 _.last() returns the last element in an array  Can be used with a parameter to return the last n elements  _.rest() returns all elements except the first one  Can be used with a parameter to return everything after the first n Arrays: last() and rest() var numbers = [1, 2, 3, 4, 5]; var initial = _.rest(numbers); // [2, 3, 4, 5] var initialTwo = _.rest(numbers, 2); // [3, 4, 5] var numbers = [1, 2, 3, 4, 5]; var first = _.last(numbers); // 5 var firstTwo = _.last(numbers, 2); // [4, 5]

_.last() and _.rest() Live Demo

Object Extensions

 Object extensions provide some additional functionality to regular objects  _.keys(obj) – list of all the keys of an object  _.values(obj) – list of the values of an object  _.invert(obj) – inverts the keys and the values  _.extend(obj, properties) – performs prototypal inheritance Object Extensions

Live Demo

? ? ? ? ? ? ? ? ? Web Storage and Cookies

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 41  Attribution: this work may contain portions from  "JavaScript Applications" course by Telerik Academy under CC-BY-NC-SA licenseJavaScript ApplicationsCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg