Skip to content

Instantly share code, notes, and snippets.

View infantiablue's full-sized avatar
🎯
Focusing

Truong Phan infantiablue

🎯
Focusing
View GitHub Profile
@infantiablue
infantiablue / array_handler.js
Last active December 22, 2015 17:29
JavaScript: Array Handler
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
// Example
function sum(numbers) {
var total = 0;
forEach(numbers, function (number) {
total += number;
@infantiablue
infantiablue / gist:9778053
Created March 26, 2014 07:01
PHP Simple Mail
<?php
$to = "dangtruong@gmail.com";
$header = "From: {$to}";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
@infantiablue
infantiablue / models.py
Created May 26, 2020 00:48
create customize form template in django - models
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(upload_to='users/%Y/%m/%d/', default='/default_avatar.png', blank=True)
def __str__(self):
return f'Profile for user {self.user.username}'
@infantiablue
infantiablue / forms.py
Created May 26, 2020 00:52
create customize form template in django - forms
class ProfileEditForm(forms.ModelForm
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['photo'].widget.template_name = 'core/widgets/photo.html'
class Meta:
model = Profile
fields = ('date_of_birth', 'photo')
@infantiablue
infantiablue / photo.html
Created May 26, 2020 00:54
create customize form template in django - widget
<div>
{% if widget.is_initial %}
<a href="{{ widget.value.url }}"><img src="{{ widget.value.url }}" width="auto" height="auto" alt="" /></a>
{% if not widget.required %}
<br />
{{ widget.clear_checkbox_label }}:<input type="checkbox" name="{{ widget.checkbox_name}}" id="{{ widget.checkbox_id }}">
{% endif %}<br />
{{ widget.input_text }}:
{% endif %}
<br />
import { createStore } from "vuex";
import axios from "axios";
const api = axios.create({
baseURL: "https://hacker-news.firebaseio.com/v0/",
});
export default createStore({
state: {
items: [],
<template>
<div v-if="items.length">
<div class="rounded-md bg-blue-50 px-3 py-1 my-1 h-auto" v-for="item in items" :key="item.id">
<div class="flex flex-wrap">
<a class="flex-auto font-sans text-black" target="blank" :href="item.url">{{ item.title }}</a>
<div class="text-sm font-medium text-gray-500">{{ item.time }}</div>
<div class="w-full flex-nonefont-semibold text-yellow-500 mt-2">{{ item.by }}</div>
</div>
</div>
</div>
<template>
<div class="container mx-auto px-4 text-left py-10">
<div class="flex">
<div class="flex-auto">
<h1 class="text-red-500 text-3xl">Latest</h1>
<Stories :items="items"></Stories>
</div>
<div class="flex-auto"></div>
</div>
</div>
@infantiablue
infantiablue / coalescing.js
Last active January 14, 2021 06:01
The simple notes about what is Javascript and how it works, from legacy to modern
console.log(true ?? "not defined") // true
console.log(false ?? "not defined") // false
console.log(undefined ?? "not defined") // "not defined"
console.log(null ?? "not defined") // "not defined"
//Compared with the OR operator ||
console.log(true || "not defined") // true
console.log(false || "not defined") // "not defined"
console.log(undefined || "not defined") // "not defined"
console.log(null || "not defined") // "not defined"
@infantiablue
infantiablue / callback.js
Created January 14, 2021 06:02
Callback example
// Callback Function Example
function greet(name, myFunction) {
console.log("Hello World");
// execute callback function only after the greet() is executed
myFunction(name);
}
// callback function
function callNameBack(name) {
// calling the function after 2 seconds