Wednesday, March 31, 2010

jQuery with rails

Hi all,

I have used the jquery js prototype recently, it has lots of more usage than our prototype.js. Easy to code using jQuery.js.

But in my rails application i have used both prototype.js & jquery.js. In this situation the $ gets conflict. To avoid conflict, use the line of code

First include prototype.js, then add jQuery.js after that you must add this line to avoid ($)conflict


Once you done this, you can enjoy the usage of both prototype.js & jQuery.js.

i.e., both $ and $j is available to access.

$('id_of_dom_elemnt') => returns (prototype)object of the html element
$j('#id_of_dom_element') => return (jquery)object of the html element

In jQuery, we have to add '#' symbol with id of dom element to find the element.

Enjoy coding with jQuery...

thanks,

Saturday, January 23, 2010

JSON startup

JSON - javascript object notation

To know about the JSON, first go through the following basics in js.

Array:

var arr = new Array("first", "second", "third", "fourth");

Retrieve value from array

arr[0] => "first"
arr[1] => "second"


Object:

var sample_object = {
"name1" : "value1",
"name2" : "value2",
"name3" : "value3",
"name4" : "value4"
}

sample_object.name1 => "value1"
sample_object["name1"] => "value1"



Objects in Array

var object_array = [
{
"first_element" : "first_value"
},
{
"second_element" : "second_value"
}
]
object_array[0] => returns first object
object_array[1].second_element => second object value


Arrays in Object:

var sample_object = {
"name1" : "value1",
"name4" : [1,2,3,4]
}
sample_object.name4 => [1,2,3,4]
sample_object.name4[0] => 1

var str_json = JSON.stringify(sample_object);
var js_object = JSON.parse(str_json);
JSON.parse(strJSON) - converts a JSON string into a JavaScript object.
JSON.stringify(objJSON) - converts a JavaScript object into a JSON string.

Monday, December 7, 2009

Watir tips

Hi Guys,

Waitr is a ruby gem used in testing. It has many uses, mostly it is used to control the web browser(all html dom elements as objects).

installation command:-
gem install waitr

Some sample use of watir

1. Starting a new Web browser
require 'watir'
ie = Watir::Browser.new
ie.goto("http://www.google.com")

2. Starting a firefox browser
require 'watir'
Watir::Browser.default = "firefox"
ff = Watir::Browser.start("http://www.google.com")

3.Setting a text field with some value

ie.text_field(:name, "username").set "Vaddi"

4. Setting value of select list

ie.select_list( :name , "category").select("Comedy")

For all the DOM elements, it has objects(for text box - 'text_field', for drop down - 'select_list')

thanks,

Friday, December 4, 2009

for loop - js

In javascript, to read the array index and their appropriate value.

Use the following way,

var a = Array.new(); declare new array with name 'a'
a = [1,2,3,4,5]; assign some values to array
var indexes = function show(value, indx){
alert(indx);
alert(value);
}
a.forEach(indexes);
'forEach' method is used to collect the values from the array.

By using the method 'forEach', we can collect each element from array.

Try this..

Monday, November 30, 2009

Add favicon image to your site

Its very easy to add favicon to ur site by following steps

include the line in your layout head
html link tag
with attributes
rel="icon" href="favicon_path"
type="image/x-icon"

favicon_path = image path of favicon

That's it...

Try with this..

Friday, November 27, 2009

has_many_friends association

Hi Guys,

I have stuck while building the association for model user,
user has_many friends, friend - nothing but a user

For that i have created a model called 'Friendship', user to join user and friends. And build the association with the following relations

friendship table has the following fields
user_id
friend_id

class User < ActiveRecord::Base
has_many :friendships
has_many :friendships_by_me, :foreign_key => 'user_id', :class_name => 'Friendship'
has_many :friends_by_me, :through => :friendships_by_me, :source => :friendshipped_by_me
has_many :friendships_for_me, :foreign_key => 'friend_id', :class_name => 'Friendship'
has_many :friends_for_me, :through => :friendships_for_me, :source => :friendshipped_for_me
end

class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friendshipped_for_me, :class_name => "User", :foreign_key => 'user_id'
belongs_to :friendshipped_by_me, :class_name => "User", :foreign_key => 'friend_id'
end

Thats it!!!

Now run the following commands

user = User.first
user.friends_by_me => [users] returns the collection of friends added by user.
user.friends_for_me => [users] returns the collection of friends who add user as their friend.

Note: i have referred the plugin 'has_many_friends'.

Thanks,
vaddi

Sunday, November 15, 2009

Drop down methods

java script methods for drop down.

selectedIndex - to get the selected index value
options[].value - to get the value of the selected item
options[].text - to get the option for the selected item

ex:


var list_box = document.getElementById('id_of_dropdown_box');
list_box.selectedIndex; --> returns the value of selected index(drop down index starts from 0) '1' check

options[0].value --> value of the zero th element '1' check

options[0].text --> text of the zero th element 'Ruby' check