Halaman

Rabu, 09 Februari 2011

jsfiddle a great way to share your js code

http://jsfiddle.net

I'll also add some demo in several of my posts about js.

Post with demo included (will be updated):
- http://tech.maysora.com/2011/02/identify-object-type-in-javascript.html

jquery programming and jquery plugin authoring commandments

jQuery Commandments

You shall not instantiate more than one jQuery object containing the same set of nodes.,

You shall not re-query the DOM for a given selection at any time unless it is suspected that the returned collection will have changed since the last query.,

You shall place scripts dependent on the readiness of the DOM at the bottom of the document.,

You shall favor CSS3 selectors over jQuery's psuedo-classes and special filters.,

You shall not use browser detection or browser inference.,

You shall use the correct spelling of jQuery and honour the capitalisation of its Q in any correspondence relating to it.,

You shall isolate any issues you have before posting to IRC or a forum, and especially before opening a ticket in the bug tracker.,

You shall, when requiring a reference to a DOM node created by yourself, gain that reference prior to DOM injection.,

You shall favor event delegation when the event in question propagates and when there are a substantial number of elements to be bound to.,

You shall not use jQuery when a faster and terser solution exists in the native DOM API.


jQuery Plugin Authoring Commandments

You shall return a jQuery object unless the method is intended to return something else.,

You shall return the instance on which the method is being called unless you wish to mutate the collection, in which case you should use the pushStack method to return a new jQuery instance.,

You shall make configuration accessible under the plugin's namespace.,

You shall make it possible to override the configuration on a per-call basis.,

You shall not use the globally defined dollar identifier.,

You shall not pollute namespaces unrelated to the plugin.,

You shall namespace any events binded to by the plugin.,

You shall remember that one configurable callback function is better than ten specific configuration options.,

You shall document the plugin.,

You shall respond gracefully should a jQuery instance contain no elements.


source: https://github.com/jamespadolsey/jQuery-Commandments

Selasa, 01 Februari 2011

[shared] identify object type in javascript

UPDATE: i changed the function name to $.typeOf(obj) to prevent ambiguity with css className, please use extendedhelper 2.4b or later

untuk sebagian besar object kita dapat menggunakan typeof, contoh:
typeof object; // will return object class name in string

tetapi untuk bbrp object seperti Array, Date, window, document dll, typeof akan mengembalikan "object" bukan class yg seharusnya, untuk mengakalinya kita dapat menggunakan Object.constructor, contoh fungsi lengkapnya:

$.getClassName = function(obj){
  try{
    var constructorStr = obj.constructor.toString();
    var tempMatch = constructorStr.match(/function (.+)\(/);
    if(!tempMatch || tempMatch.length != 2) tempMatch = constructorStr.match(/ (.+)\]/);
    return tempMatch[1];
  }catch(e){
    return (typeof obj).capitalize();
  }
}

$.getClassName(1); // -> 'Number'
$.getClassName('Aya Hirano'); // -> 'String'
$.getClassName(['A','H']); // -> 'Array'
$.getClassName(null); // -> 'Object'
$.getClassName(undefined); // -> 'Undefined'
$.getClassName(new Date); // -> 'Date'
$.getClassName({'aya':'forever'}); // -> 'Object'
$.getClassName(document); // -> 'HTMLDocument'
$.getClassName(window); // -> 'Window'
$.getClassName(document.getElementsByTagName); // -> 'Function'
$.getClassName(document.getElementsByTagName('div')[0]); // -> 'HTMLDivElement'

fungsi getClassName tersebut ada di jquery.extended_helper 2.4 dan menggunakan fungsi capitalize dari js tersebut.

Demo

Combining jquery fullcalendar with jquery qtip

buat ntar aj lah.. lg males..

Rabu, 22 Desember 2010

[shared] change rails form error field wrapper

by default rails wrap all fields that contain error data using div with "fieldWithErrors" class

but sometimes we want to change that wrapper for example to use span instead of div.
that can easily be done by overriding field_error_proc class attribute in ActionView::Base

in environment.rb
ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}".html_safe }

file position (for rails 2.3.10):
[ruby_dir]\lib\ruby\gems\1.8\gems\actionpack-2.3.10\lib\action_view\helpers\active_record_helper.rb