Skip to content

Instantly share code, notes, and snippets.

@khoand0000
khoand0000 / shared.js
Last active December 25, 2015 10:49
shared.js
function extractFormData(selector, data) {
$.each($(selector).serializeArray(), function (i, field) {
data[field.name] = field.value;
});
}
/**
* usage: var $arr = {a: 1, b: 4}; empty($arr['test']); // true
*/
function empty($var) {
@khoand0000
khoand0000 / jquery-data.md
Created December 7, 2015 09:55
Using .data() tie data/information to element instead of using variable

Using .data() tie data/information to element instead of using variable.

@khoand0000
khoand0000 / ui-sref-is-not-dynamic.md
Created December 6, 2015 19:25
ui-sref is not dynamic. using ng-click() with $state.go() instead

My situation is that: I write a web-app like gmail: normal messages, starred messages, label messages sections (3 states). They have same functions except different webservice calling. But format of their result are same:

{
  "result": [],
  "count": 5
}

I want to use only controller for 3 states. So I use resolve to achieve that.

@khoand0000
khoand0000 / ng-if-with-directive.md
Last active December 6, 2015 18:13
using ng-if and ng-show with directive

Carefully when using ng-if with directive. My situation is that I want to show my directive when it meet a condition. There are 2 ways achieve it:

  • Solution 1: using ng-if when using my custom directive. The solution is working normally. --> UPDATE 2015-12-07: use the solution
<!-- template.html: I use `directive` here -->
<!-- data is array, I will show array if data.length > 0-->
<my-custom-directive ng-model="data" ng-if="data.length > 0"></my-custom-directive>
@khoand0000
khoand0000 / re-use-css-code-rules.md
Created December 5, 2015 16:35
Rules when reusing css code from existing code

I have css code apply html code. Now, I want to re-use it but customizing it. Rule is that always add more class into new code instead of changing old code (need to change everywhere). Example:

<div class="box">
  <div class="header">header</div>
  <div class="body">body</div>
  <div class="footer">footer</div>
</div>
.dropdown:hover .dropdown-menu {
display: block;
margin-top: 0; /* remove the gap so it doesn't close*/
}
@khoand0000
khoand0000 / stop-propagate-event.md
Created December 3, 2015 20:27
stop propagate event

I have <button> is children <a>, I don't want to make click event of <a> occurs when clicking <button>

<a href="#">
  <button>do something</button>
  sometext
</a>

Solution 1: return false; in click handler of ``

@khoand0000
khoand0000 / delegate.md
Created December 3, 2015 20:14
Using delegate() attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

Case: you have a <select>, when it is changed, creating new elements (example: <a>), and you want to handle click event of new <a>

<select id="chooser">
  <option value="1">Restaurants</option>
  <option value="2">Hotels</option>
</select>

<div id="result">
</div>