Skip to content

Instantly share code, notes, and snippets.

View khoinv's full-sized avatar
🏠
Working from home

Nguyen Van Khoi khoinv

🏠
Working from home
View GitHub Profile
function PubSub() {
const callbacks = {};
function on(event, callback) {
callbacks[event] = callbacks[event] || [];
callbacks[event].push(callback);
}
function emit(event, ...data) {
callbacks[event] && callbacks[event].forEach(function (callback) {
function throttle(callback, limit) {
let wait = false;
return function () {
if (!wait) {
callback.apply(null, arguments);
wait = true;
setTimeout(function () {
wait = false;
}, limit);
}
function debounce(fn, delay) {
let timer = null;
return function () {
const context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
/**
* Dùng để add {template}
*
* Example:
* template: <div class="hide" id="template">{Somethinghere}</div>
* add new choice button: <button class="btn btn-primary js-choice-add" data-ref="#template">Add new choice</div>
*
* usage $('.js-choice-add').addByTemplate();
*/
$.fn.addByTemplate = function(){
/**
* Hiển thị preview ở cho input.
*/
$.fn.inputPreview = function(options = {
listen_in_runtime: true
}){
// Validation-start
const $this = $(this);
const ref = $this.data('ref');
if (!ref) {
/**
* Khi dữ liệu được tham chiếu tới một trường khác thì khai báo theo mẫu sau.
* <div data-ref={ref}><input></div>
*
* @ref: chỉ tới phần tử DOM, nơi cần tham chiếu.
*/
$.fn.inputRef = function(){
// Validation-start
const $this = $(this);
const $input = $this.find('input');
// Create new Error type
function JQueryPropError(element, message) {
const instance = new Error(message);
instance.name = 'JQueryPropError';
instance.element = element;
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(instance, JQueryPropError);
}
return instance;
@khoinv
khoinv / ProfileJsonResponse.php
Last active November 30, 2018 03:27
Laravel Api profiling with duplication queries information.
<?php
declare(strict_types=1);
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\JsonResponse;
/**
* Class ApiStaffMiddleware
@khoinv
khoinv / AssetController.php
Last active December 29, 2017 09:31
laravel helpers
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use function App\Helpers\get_lang_dot_js_content;
/**
* Class AssetController