diff --git a/3 JavaScript/docs/13 - Vue Basics.md b/3 JavaScript/docs/13 - Vue Basics.md new file mode 100644 index 00000000..8de0a009 --- /dev/null +++ b/3 JavaScript/docs/13 - Vue Basics.md @@ -0,0 +1,153 @@ +# Vue + +Vue is a JavaScript framework for building user interfaces. Vue is a MVVM framework (Model View View-model). This is a fancy +way of saying that you give Vue a JavaScript object that represents the state (or data, or model) of your application and a +template (or view) of the visual representation of your data. Vue provides a view-model that binds your data to your template +and syncs them to each other instantly as both update. This means that as the user updates the values of elements on the page +they are instantly reflected in the data object, and as you update the data object with event listeners or other methods the +webpage template is instantly re-rendered with the new data. This removes the need to do manual DOM manipluation, making it +easy to write more complicated user interfaces and interactive web pages. + +For more information about JavaScript frameworks and why to use them, go [here](https://www.academind.com/learn/javascript/jquery-future-angular-react-vue/). + +More detailed explanations and code examples can be found in the Vue guide [here](https://vuejs.org/v2/guide/). + +A YouTube series on Vue basics can be found [here](https://www.youtube.com/watch?v=5LYrN_cAJoA&list=PL4cUxeGkcC9gQcYgjhBoeQH7wiAyZNrYa) and the solution repo [here](https://github.com/iamshaunjp/vuejs-playlist/tree/lesson-1). + +## Installing + +Vue can be run from a CLI tool and it's own project like Django, but in class we will be running Vue from a CDN, loaded +in with a `script` tag. This means we can add Vue to an existing HTML page and add Vue functionality and features +at our own page, on top of our existing front-end knowledge. + +`` + +Also highly recommended: +* [Vuetur syntax highlighting and snippits for VS Code](https://marketplace.visualstudio.com/items?itemName=octref.vetur) +* [Vue devtools for Chrome/Firefox/etc](https://github.com/vuejs/vue-devtools) + +## Declarative Rendering + +``` +
{{ message }}
+ +{{ message }}
+ +${response.quote.body}
+ + ` + textTarget.innerHTML = resultHTML; +}); + +... +``` + +### 4. Update the other listeners too + +```javascript +... + +req.addEventListener("progress", function(e) { + console.log(e.loaded); + target.innerText = "Loading..."; +}); +req.addEventListener("error", function(e) { + console.log(e.status); + target.innerText = "Cannot load quote. Try again later!"; +}); +req.addEventListener("load", function(e) { + ... +}); + +... +``` + +### 5. Use an event to fire the request + +Here's our code so far: + +```javascript +let target = document.getElementById("target"); + +let req = new XMLHttpRequest(); +req.addEventListener("progress", function(e) { + console.log(e.loaded); + target.innerText = "Loading..."; +}); +req.addEventListener("error", function(e) { + console.log(e.status); + target.innerText = "Cannot load quote. Try again later!"; +}); +req.addEventListener("load", function(e) { + console.log(req.responseText); + let response = JSON.parse(req.responseText); + console.log(response); + + let resultHTML = ` +${response.quote.body}
+ + ` + textTarget.innerHTML = resultHTML; +}); +req.open("GET", "https://favqs.com/api/qotd"); +req.setRequestHeader("Authorization", 'Token token="YOUR_TOKEN_GOES_HERE"'); +req.send() +``` + +Right now, our Ajax request is sent immediately as the page loads. This usually isn't the desired behavior. Most of the time an Ajax request is sent as the result of some sort of user input. I have a "get quote" button on my page, and I'm going to make this Ajax request send when the "get quote" button is pressed by the user. First, select the element and add an event listener. + +```javascript +let target = document.getElementById("target"); +let getQuoteButton = document.getElementById("quote-button"); + +getQuoteButton.addEventListener("click", function(e) { + // code goes here +}); +``` + +Next, put the entire body of code for making, parsing, and using a request inside the event listener. Now the page will only make the request and update the DOM using the response when the "get quote" button is clicked. + +```javascript +let target = document.getElementById("target"); +let getQuoteButton = document.getElementById("quote-button"); + +getQuoteButton.addEventListener("click", function(e) { + let req = new XMLHttpRequest(); + req.addEventListener("progress", function(e) { + console.log(e.loaded); + target.innerText = "Loading..."; + }); + req.addEventListener("error", function(e) { + console.log(e.status); + target.innerText = "Cannot load quote. Try again later!"; + }); + req.addEventListener("load", function(e) { + console.log(req.responseText); + let response = JSON.parse(req.responseText); + console.log(response); + let resultHTML = ` +${response.quote.body}
+ + ` + textTarget.innerHTML = resultHTML; + }); + req.open("GET", "https://favqs.com/api/qotd"); + req.setRequestHeader("Authorization", 'Token token="YOUR_TOKEN_GOES_HERE"'); + req.send() +}); +``` + +## Advanced + +### Fetch and ES6 Promises +[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) provides a newer, easier-to-use method to send HTTP requests and process their responses compared to `XMLHttpRequest`. + +Here's how to send a `GET` request and process it into JSON: +```js +fetch('https://api.ipify.org/?format=json') + .then(function(response) { + return response.json(); + }) + .then(function(myJson) { + console.log(myJson); + }) + .catch(error => console.error(error)); +``` +This is a basic `GET` request sent to `https://api.ipify.org/?format=json`. `fetch()` uses JS **Promises** to handle processing the request asynchronously. Any `.then(callback)` calls make sure to only process the callback after the previous function has been completed. `.catch(callback)` handles any errors returned from the request. The simplest use of `fetch()` takes one argument — the path to the resource you want to fetch — and returns a Promise containing the response (a Response object). + +#### Request options +The fetch() method can optionally accept a second parameter, an object that allows you to control a number of different settings. Some common parameters are below with their optional values. You can read more about others [**here**](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch). + +- method: [\*GET, POST, PUT, DELETE, etc.] +- mode: [no-cors, cors, \*same-origin] +- cache: [\*default, no-cache, reload, force-cache, only-if-cached] +- credentials: "same-origin", [include, same-origin, \*omit] +- redirect: [manual, \*follow, error] +- referrer: "no-referrer", // no-referrer, *client +- body: The body you want to add to your request (not available for GET or HEAD requests) +\*Note: Starred options are the default setting. + +This example is sending a `POST` request with some form data. +```js +let header = new Headers() +header.append('Authorization', 'Token token="
+```
+
+## Template Inheritance: `block` and `extend`
+
+You can have one template 'inherit' from another, meaning the child template's content will be included inside the parent. You can accomplish this by putting a `{% block content %} / {% endblock %}` in the parent and an `{% extends 'this is the page content for the index page
+{% endblock %} +``` + +**detail.html** +````html +{% extends 'myapp/base.html' %} +{% block title %}Details{% endblock %} +{% block content %} +this is content for the detail page
+{% endblock %} +```` + +## Filters + +Filters allow you to change how values are rendered in the template. + + diff --git a/4 Django/docs/05 Forms.md b/4 Django/docs/05 Forms.md new file mode 100644 index 00000000..fc17ba48 --- /dev/null +++ b/4 Django/docs/05 Forms.md @@ -0,0 +1,149 @@ + + +# Forms + +- [Overview](#overview) +- [Django Forms](#django-forms) + - [The ModelForm Class](#the-modelform-class) + - [Using Forms with CSS Frameworks](#using-forms-with-css-frameworks) + + + +## Overview + +A `form` is an HTML element that can transmit data from the front-end (client) to the back-end (server). Read more about forms [here](../../2%20Flask%20+%20HTML%20+%20CSS/docs/11%20HTML%20Forms.md). There are 5 important parts to a form: + +1. The `action` is the path or url to which the form's data will be submitted. +2. The `method` is the HTTP method to send the request in (POST, GET). +3. The `input` elements inside a form need name attributes, which will be used to retreive the data on the back-end. +4. The `