Skip to content

Instantly share code, notes, and snippets.

View garethredfern's full-sized avatar
🔥

Gareth garethredfern

🔥
View GitHub Profile
@garethredfern
garethredfern / using-javascript-to-access-url-segments.md
Created November 16, 2019 12:38
Using JavaScript To Access URL Segments :: 2012-06-19

Whilst working on a recent project, I wanted an accordion navigation to remain open depending on what the category segment in the url was displaying. To do this I found a simple bit of JavaScript published on CSS-Tricks which looks like this:

Get the full URL path:

var newURL = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;

Next split the segments of the URL using:

@garethredfern
garethredfern / understanding-closures-in-javascript.md
Created November 16, 2019 12:36
Understanding Closures In JavaScript :: 2016-03-29

This example shows how you create a closure in JavaScript it uses an alert function that can be incremented and reused/passed around.

The Problem

The following code will alert the number 3 three times. You may have expected it to alert 0,1,2 (don't forget JavaScript is zero based).

var a = {};
for (var i = 0; i < 3; i++) {
    a[i] = function() {
    alert(i);
};
@garethredfern
garethredfern / factory-function-to-create-an-object-in-javascript.md
Created November 16, 2019 12:35
Factory Function To Create An Object In JavaScript :: 2016-04-09

Use a constructor function that returns an object. You can then create multiple people by passing in the first and last name arguments the createPerson function.

var createPerson = function(firstName, lastName) {
    return {
        firstName: firstName,
        lastName: lastName,
        sayHi: function() {
        return "Hi there";
 }
@garethredfern
garethredfern / basic-module-pattern-javascript.md
Last active November 16, 2019 12:33
Basic Module Pattern JavaScript :: 2016-04-10

Wrap your code in an immediately invoked function expression (IFFE). It runs immediately when you create it and has no name. Normally you would create a IFFE like this:

(function() {

// your code

}());
@garethredfern
garethredfern / javascript-objects.md
Last active November 16, 2019 12:39
JavaScript Objects :: 2016-04-10

In this example we are storing the object's properties by value. What this means is that cb is stored in memory, when you change the value of box.material to Steel it doesn't change the value of cb. Both the console.log(cb); will display cardboard as cb has stored the value cardboard in memory.

var box = {};

box.material = "Cardboard";

var cb = box.material;

console.log(cb);
@garethredfern
garethredfern / passing-arguments-in-javascript.md
Last active November 16, 2019 12:39
Passing Arguments in Javascript :: 2016-04-10

You can pass arguments into functions to be used within the function. These arguments can be any JavaScript data type including functions.

  • We create an ifElse function which has a condition of true or false passed into it, 2 functions and 1 argument to be used in those functions.
  • Notice that funcOne and funcTwo both take an argument of x which is console logged when they are called.
  • We call the ifElse function and pass in true as the condition, the two functions and a string of myArg.
  • The condition is true so isTrue is called and the myArg string gets console logged as it was passed in via the arg argument.
var ifElse = function(condition, isTrue, isFalse, arg) {
 if (condition) {
@garethredfern
garethredfern / update-a-user-profile-in-laravel.md
Last active November 16, 2019 12:40
Update A User Profile In Laravel :: 2016-04-25

I ran into a problem the other day when I was trying to create a page where an admin could update user details in a dashboard area of an app. I needed the user to be able to update an email address and name field. The issue was that the email address would update no problem but the name wouldn't. Here is the code:

The Edit Form

<form method="POST" action="{{ route('users.update', $user->id) }}">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <input type="hidden" name="_method" value="PUT">
  <div class="form-group">
    <label for="name">Name</label>
@garethredfern
garethredfern / laravel-paginator.php
Last active September 2, 2019 04:29
Laravel manual paginator example.
// use Illuminate\Pagination\LengthAwarePaginator;
$data = collect($matrixReportService->matrixReport($request));
$perPage = 5;
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$currentPageSearchResults = $data->slice(($currentPage - 1) * $perPage, $perPage)->all();
$entries = new LengthAwarePaginator($currentPageSearchResults, count($data), $perPage);
return response()->json($data, 200) ;
@garethredfern
garethredfern / AppServiceProvider.php
Created February 12, 2019 08:45 — forked from simonhamp/AppServiceProvider.php
A pageable Collection implementation for Laravel
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class AppServiceProvider extends ServiceProvider
{
public function boot()