jQuery validation errors with custom select wrappers

This was easy.

If an errored element has a parent called .custom-select, then insert after the custom select wrapper. Otherwise, proceed as usual.


$form.validate
errorPlacement: (error, element) ->
inserted = false
if <a href="http://element.is">element.is</a>('select')

customSelect = element.closest('.custom-select')
if customSelect.length &gt; 0
error.insertAfter(customSelect)
inserted = true

if not inserted
error.insertAfter(element)

How to detect if a click event was triggered by the user or programmatically

I often set up click handlers, then trigger it programmatically to set its default state (say an option click handler, where the first option is selected by default). This is a good pattern because you don’t end up with maintaining state via template and JS separately.

It’s also very common to want the programmatic click to happen very quietly, for example without scrolling the user down whereas a user click _should_ scroll the user down.

You simply need to check for the value of `event.originalEvent`.

if ev.originalEvent
console.log &quot;This is a user event!&quot;

Cool.

Nginx forward all HTTP headers through proxy

If you’re wondering where your header went, consider if it contains underscores.

Nginx needs the underscores_in_headers setting on to handle underscores in header names correctly. No need to set manual proxy header forwarding, just set this flag:

underscores_in_headers on;

jQuery Validation Easy Custom Validation Exceptions

Here’s a method I just used to add some very / arbitrarily complex validation logic to your existing jQuery Validation form.

The use case was that my form, depending on arbitrary third party ajax calls, could contain custom invalid fields in ways that would be difficult to carry out through the framework itself.

Why not offload all of that logic to some third party, like the server, and then have some javascript simply set data attributes on the form elements which mark it as "invalid"?

That’s exactly what I did.

For example, my server could claim that the ship_state field is invalid for various reasons. All my AJAX needed to do was set a data attribute on that field called `custom-validator-exception`, with the value as the error message, and the validation framework seamlessly incorporates that error into the workflow.

https://gist.github.com/yuchant/955414c26733adfc1e25

Awesome!

Force jQuery LazyLoad to Appear

Sometimes, you need to manually trigger the lazyload even though you want it to respond to scroll events normally.

Simply trigger the `appear` event to force lazyload to load the image.

This is applicable in my case when for example I am using lazyload on a gallery slider where the user may scroll down to it, or use a button to trigger the new slides to appear.

Example:

$("img.lazy").trigger(‘appear’);

The answer was in the source. Always look in the source.

How to sort a python list by the order of matching values in another list

This is not an easy question to ask: I am talking about ordering list A by the order of matching values found in list B.

Most results talk about ordering list A by an ordering field in list B.

After some thinking, it became really obvious how to do this.

Simply create an empty list, find the index of an item in list A of list B.

list_a = [100, 1, 10]
list_b = [1, 10, 100]

sorted_list_b_by_list_a = sorted(list_a, key=lambda x: list_a.index(x))

Select2, autocomplete in the Django-Suit admin

Having auto-fill select widgets is a night and day improvement in UX for large select boxes.

Adding it to django suit was easy, but surprisingly not documented.

It has been easy to add Chosen or Select widgets via the popular django integration packages, but it failed to work on inlines.

To make that as easy as possible, I just decided to manually convert all select widgets on the screen into select2 widgets via a form include.

It was quite simple to hook into Suit’s add-inline event listener and remove the old select classes and adding new ones.

It should be a breeze to localize this if you are having it applied to random pieces.

https://gist.github.com/yuchant/d935208978a1fc6e3098

How to stack elements on top of each other without absolute positioning

This is extremely useful if you need to preserve height flow, but need to stack elements for the all too common use case of cross fading between elements.

Simply apply relative positioning, float the element, -100% margin left, and left of 100%. Apply clearfix to your parent, and now you have stacked elements that are still contribute to vertical flow.

Amazing.

.configuration-image-wrapper {
@include clearfix;
img {
position: relative;
top: 0;
left: 100%;
margin-left: -100%;
float: left;
}
}

Linode.com is an absolutely amazing VPS provider

Linode.com has always been great. Support. Always at the cutting edge.

Imagine my surprise when I’m checking out the processor on my VPS because I randomly saw it’s a dual core… which is unusual.

Then, I look it up on a benchmark site (because you can’t judge CPUs by clock speed or model numbers).

This CPU is the third fastest one in the world right now and costs nearly $2k.

http://www.cpubenchmark.net/cpu.php?cpu=Intel+Xeon+E5-2680+v2+%40+2.80GHz&id=2061

Absolutely crazy. I would never expect to get one so fast…

Here’s my Linode affiliate link.

https://www.linode.com/?r=964d9fe684204460dd42f36002f33ad337460423