noob alert!: I am pretty new to RoR. so i might not be solving the problem with the best possible solution.
Scenario 1: many-to-many + one-to-many
Task: I have two models. Contact and ContactSource. a Contact belongs to a ContactSource. and a Contact can be synced with one or more ContactSource(s). i need to get the ContactSource(s) with whom a given Contact is synced ex:- Contact1.synced_with => [ContactSource1, ContactSource2...]
Solution: for the first relationship(one-to-many) we can use belongs_to. and for second we can use has_many with :through
class Contact :contacts_synced_with_contact_sources end class ContactsSyncedWithContactSource < ActiveRecord::Base belongs_to :contact belongs_to :contact_source end
In the above code we have no problem accessing the data relevant to the two relationships. because: Contact.contact_source would point to the Contact Source who owns the Contact while Contact.contact_sources would point to the ContactSources with whom the Contact is synced with. but look at the next problem….
Scenario 2: many-to-many + many-to-many
Task: I have two models. Book and Student. a student can own many books, while a book(this does not refer to a individual copy of a book, but to all the copies of a book) can be owned by many students. and a student have a collection of books that he loves.
Problem: here we have two many to many relationships within two business entities. if you try to solve this with has_many :through or has_and_belongs_to_many, you would run in to a problem. in the first problem you referred to the ContactSources by using the property accessor contact_sources. but here if you try using student1.books how would ActiveRecord know what to return?(book the student love or the books he own?).
you could solve this problem by using the :source option(is that what you call these in ruby?
) in has_many… it would like this…
has_many :books_i_love, :source => "book", :through => :student_love_books
you can give a custom property accessor name such as books_i_love but then you have to use the :source => “book” to let rails know about what model(s) you are trying to access.
if you do not use the :source => “book” you would get a very helpful error message like this
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :books_i_love or :books_i_love in model StudentLoveBook. Try 'has_many :books_i_love, :through => :student_love_books, :source => '. Is it one of :student or :book?
source follows…
class Student "book", :through => :student_love_books
has_many :student_own_books
has_many :books, :through => :student_own_books
end
class Book < ActiveRecord::Base
end
class StudentLoveBook < ActiveRecord::Base
belongs_to :student
belongs_to :book
end
class StudentLoveBook < ActiveRecord::Base
belongs_to :student
belongs_to :book
end
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :books
end
end
class CreateStudents < ActiveRecord::Migration
def self.up
create_table :students do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :students
end
end
class CreateStudentOwnBooks < ActiveRecord::Migration
def self.up
create_table :student_own_books do |t|
t.column :student_id, :integer
t.column :book_id, :integer
t.timestamps
end
end
def self.down
drop_table :student_own_books
end
end
class CreateStudentOwnBooks < ActiveRecord::Migration
def self.up
create_table :student_own_books do |t|
t.column :student_id, :integer
t.column :book_id, :integer
t.timestamps
end
end
def self.down
drop_table :student_own_books
end
end
hope this would help someone/myself in the future
Think: In the Entity Framework…
you have two models like this:
Item{ID, Name, Category}, Category{ID, Name}
With a Relationship like this:
Item belongs to a Category. and a Category may have zero or more Items.
if you try to sort the Items by their Category(which doesn’t make sense in the first place!
) by doing something like:
YourObjectContext.Items.OrderBy(“it.Category”)
you would get the error that is specified in the title of this post. .
but if all you want to do is sort all the Items by the Name of their respective Categories. you should do something like this:
YourObjectContext.Items.OrderBy(“it.Category.Name”)
it will work just fine. you can use this doted notation to specify properties of properties(nested properties). this can be used with OrderBy’s Where caluses…etc this is a faily simple thing i know
but it was bit tricky to find this out, for me atleast.

arch linux - kde - yakuake
Background
I’ve been using linux for like 4 years now. in the beginning i was just trying it out. back then i used almost any thing i could find. redhat/knoppix/suse/debian…etc but back then i was only trying them out. but for the last one/two year(s) I’ve been with ubuntu. the reasons were simple. it just worked! but recently i felt so bored with my system. so last weekend i download a Arch iso and started installing the distro.
Why Arch Linux
- simple
- packages are build for i686, thus they will be more optimized(good for my ‘old’ laptop)
- pacman is good(I’m a huge fan of apt, so a good package manager was a must)
- good documentation/support/community
- i can choose what i want to install, i have control over what’s getting into my system. ie: the system is really ‘mine’ when its done.

arch linux kde desktop
Installation
i read a bit of the beginners guide available in the Arch Wiki. but soon got bored
so i followed along the installation script. it was pretty straight forward. but i wanted to use ext4 as my root file system. as i was trying out a new distro. i figured this is the best time to try out the new file system. after all there was a lot of talk going around about ext4 been much better than ext3(naturally…). i was installing the system on my laptop. which is a bit old. so i thought ext4 would help me with the speed. and after all one of the reasons i choose to go with arch was that all it’s packages were build for i686 platform. which will usually make em run faster.
Arch and Ext4
as of now(16/feb/2009) official arch installing media doesn’t support ext4. meaning you wouldn’t be able to just make a ext4 partition the way you would have made a ext3… because the tools needed for it are not included in the installation media. but its not that hard to get it working. the best way to do is(ASFAIK) installing the system in to a ext3 partition. and then converting the partition(s) ext4. when you need to convert your root partition you have to use a live cd to do it. its pretty straight forward. please refer to the arch wiki for more info.
btw i tried to upgrade the live cd(arch installation media) before installing the system so i could get all the ext4 supported tools..etc but that was too good to be true. it broke the system. and when i tried to get around the broken stuff it eventually lead to a kernel panic!
but its ok. its nice to have experiences right?
KDE
i am so amazed with the latest release of KDE! it rocks. i have been a gnome(default in ubuntu) user for a while now. never had much time to look at kde. even when i got a chance to work with it. it felt a bit uncomfortable so i never really wanted to try it out. but lately i’ve been more and more curious about KDE. so this time i choose to go with it. wow. it was really nice. its visually appealing. but wait its not the main thing. its rock solid! i have only 512MB ram. but still it works grate. no slow downs or what so ever. i feel good about the way it looks and works. but there are somethings that i am still a bit alien to. but its ok. i am getting used to KDE. its grate. i suggest you give it a try too. you never know, you might like it too.

archlinux - kde - firefox - KFirefox theme
Where is Firefox?
i found it a bit hard to believe that firefox is not the default browser in KDE at first when i installed it. but then again konqueror is a decent browser too. anyways i switched to firefox. when you start up firefox you would notice something. that is it looks damn UGLY! i think this is because it is not a kde application. but no worries you can get it to look better by installing KFirefox theme. it gives a nice look. and FF would look much more like a kde app.

kde 4.2 - kickoff application launcher
Getting Stuff to Work
unlike ubuntu arch expects you to read stuff and install what you need by your self. its not hard. but it will need some of your time. you will need do some googling at some point or other and find out about things that you need and install them by your self. for example when i tried to connect to my gmail/gtalk account using kopete. it just didn’t work. reading around a bit reviled that it need a SSL library installed in order to work with gtalk. i had to install these libraries by my self. but of course its just a ‘pacman -S package’ away!
Arch WiKi Rocks!
arch wiki is a very rich source of information when you trying to solve your problems about arch. it will really teach you a lot of things about your system. whenever you want install some package or whenever you want to learn about how to do something in arch. its best to check out the wiki. as it have answers/articles for most your common problems. it will save you a lot of time.
Final Thoughts
i’m kinda happy that i choose to go ahead and install this new distro. i now have a good system running in my laptop. arch linux is a grate distro. you might want to give it a try. its not for everyone. if you don’t won’t to mess with your system and just wants something that just works. go for ubuntu/fedora/mint…etc. but if you want a system that is pretty simple and configurable to your liking. arch is a good choice. you can make a lot of choices when you choose arch. unlike in ubuntu where a lot things are already decided for you(this is good if you just want everything to work…).
I am new to all these JS libraries like jQuery and ExtJS(there are many more out there…). when i got started with asp.net mvc i looked in to jQuery. and i was absolutely amazed with all the functionality it offered with very simple and neat syntax! a while ago i did a post on “Ajax Enabling MVCContrib Grid’s Pagination with jQuery”. In which i added AJAX/pagination support to MVCContrib’s Grid. but with time i needed more and more features from the grid. like sorting and filtering(through AJAX of course). then i came across the wonderful JS library called ExtJS. it have grate UI components done and ready to be used. i love all ExtJS stuff that is related to UI. and their components seems to be very extensible as well.
Ext JS Overview
Ext JS is a cross-browser JavaScript library for building rich Internet applications. It includes:
- High performance, customizable UI widgets
- Well designed and extensible Component model
- An intuitive, easy to use API
- Commercial and Open Source licenses available
i liked there Grid and it had all the features i needed. so i decided to go ahead with it.

the main points i had to look at:
- including a ExtJS distribution with my asp.net mvc project which is already using jQuery
- inserting the relevant JS to render the grid in my views
- creating the necessary controller actions to support the grid’s actions(get data/sort/filter data…etc)
Including a ExtJS distribution
ExtJS JS library can use other libraries(such as jQuery…) as its base library. so been a fan of jQuery, i choose to build a custom version of ExtJS which uses jQuery as its base. this version of the library includes the jQuery adapter within it self. but you still need to include the jQuery library by your self. it is NOT included in the custom version that you built for your self. so in your site.master(you can put these where ever you like, not necessary in the site.master. just make sure these are included where ever you use them) you have to include the libraries in the following order: include jQuery first, then include any jQuery plugins you use and finally include ExtJS(any of your custom JS files should be included afterward).
now you are done including the JS libraries necessary . but ExtJS’s UI components need a few more things to be included in your site.master for them to work correctly. you need to include the necessary style sheets/images used by ExtJS components. to get these download ExtJS SDK. this includes all the style sheets and the images among other things. you will need to include the necessary plugins as well.
- ext-2.2resourcescssext-all.css
- ext-2.2resourcesimages – images used by ExtJS components
- all JS files under ext-2.2examplesgrid-filteringgrid – includes necessary plugins needed for the grid filtering functionality
now you are ready to roll. note that we can use what ever jQuery we want when we are working with extjs components. and as we are using the jQuery core(and also because we are already using jQuery in out mvc project) the size of the ExtJS library is much lower.
Rendering the grid
looking at the samples(look under ext-2.2examplesgrid-filtering) included in the extjs sdk. its not that hard to figure out how to render the grid using javascript. the extjs API reference would be helpful as well.
i ran across a bug in the GridView component when i tried to use disable ‘autoHeight’ ‘feature’ of the grid. how ever there was a fix available for it in the the extjs forums. following is the JS code that i used in my view(in which the grid resides). i just included them in a script tag within the view. but the best way to use them would be to extract them in to some helper methods and then using the helpers.
some notes about ExtJs grids
- a grid is ‘bound’ to a data store.
- when rendering a grid you will have to create a data record, column modal and a data reader(JsonReader in this case).
- column modal – how the columns are structured
- data record – how a data record(or a person object, in the context of this example) is structured. what fields does it contain…etc
- data reader – where to get/ how to read data
- filters and paging is connected to the grid in a ‘pluggable’ manner
//Sugguested Fix for the bug: "Grid autoHeight disables horizontal scrolling too"
Ext.override(Ext.grid.GridView, {
layout: function() {
if (!this.mainBody) {
return;
}
var g = this.grid;
var c = g.getGridEl(), cm = this.cm,
expandCol = g.autoExpandColumn,
gv = this;
var csize = c.getSize(true);
var vw = csize.width;
if (vw < 20 || csize.height < 20) {
return;
}
if (g.autoHeight) {
csize.height = this.mainHd.getHeight() + this.mainBody.getHeight();
if (!this.forceFit) {
csize.height += this.scrollOffset;
}
}
this.el.setSize(csize.width, csize.height);
var hdHeight = this.mainHd.getHeight();
var vh = csize.height - (hdHeight);
this.scroller.setSize(vw, vh);
if (this.innerHd) {
this.innerHd.style.width = (vw) + 'px';
}
if (this.forceFit) {
if (this.lastViewWidth != vw) {
this.fitColumns(false, false);
this.lastViewWidth = vw;
}
} else {
this.autoExpand();
}
this.onLayout(vw, vh);
}
});
//end bug fix here
//////////////////
Ext.onReady(function() {
Ext.QuickTips.init(); //for enabling tool tips
Ext.menu.RangeMenu.prototype.icons = {
gt: '/Scripts/extjs/plugins/grid-filter/img/greater_then.png',
lt: '/Scripts/extjs/plugins/grid-filter/img/less_then.png',
eq: '/Scripts/extjs/plugins/grid-filter/img/equals.png'
};
Ext.grid.filter.StringFilter.prototype.icon = '/Scripts/extjs/plugins/grid-filter/img/find.png';
createAndShowGrid();
});
function createAndShowGrid() {
order = Ext.data.Record.create([
{ name: 'Id' },
{ name: 'FirstName' },
{ name: 'Email' }
]);
orderReader = new Ext.data.JsonReader({
root: 'dataitems',
totalProperty: 'totalItems' //number of total records
},
order
);
//data store creation
ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: '/Person/ListJson'
}),
reader: orderReader,
sortInfo: { field: 'Id', direction: "ASC" },
remoteSort: true
});
columnModel = new Ext.grid.ColumnModel([{
header: 'Person Id',
dataIndex: 'Id'
}, {
header: 'Person Name',
dataIndex: 'FirstName'
}, {
header: 'Person Email',
dataIndex: 'Email'
}
]);
columnModel.defaultSortable = true;
var filters = new Ext.grid.GridFilters({
filters: [
{ type: 'string', dataIndex: 'Id' },
{ type: 'string', dataIndex: 'FirstName' },
{ type: 'string', dataIndex: 'Email' }
]
});
var pagingBar = new Ext.PagingToolbar({
pageSize: 10,
store: ds,
displayInfo: true,
displayMsg: 'Displaying Persons {0} - {1} of {2}',
emptyMsg: "No Persons to display",
plugins: filters
});
grid = new Ext.grid.GridPanel({
store: ds,
cm: columnModel,
width: 500,
autoHeight: true,
title: 'Persons List',
frame: true,
loadMask: true,
stripeRows: true,
plugins: filters,
bbar: pagingBar // paging bar on the bottom
});
grid.render('personGrid');
grid.store.load({ params: { start: 0, limit: 10} });
}
<div id="personGrid"
Controller actions to support the grid’s actions(get data/sort/filter data…etc)
A ExtJS grid can be fed data in various ways. i am going use a controller action to give the grid the data it needs to render it self. the data would be in json format. we would have a controller action like the following, that returns a JsonResult. we can use the Json method in the mvc framework to serialize the data to json with ease. i am doing it by constructing a anonymous type that hosts the data and then serializing it to json. please note that the following example uses the Entity Framework to deal with the back-end data. although this is example uses the EF. you can easily port it to use any other ORM technology(Linq…etc).
source of /Person/ListJson action
public JsonResult ListJson()
{
int totalOjectCount;
var personsList = ExtJSGridFilterHelper.GetResults(Request, new ObjectEntities().Person,out totalOjectCount);
var list = new ArrayList();
foreach (var person in personsList) //populate data containers with read data
{
list.Add(new
{
Id = person.ID,
FirstName = person.FirstName,
person.Email
});
}
return Json(new { dataitems = list.ToArray(), totalItems = totalOjectCount });
}
note that the json result that is returned, basically contains a array of items and a another property called totalItems. this total items property tell the grid, the number of(obviously ) total items that it will have to display. this is helpfule for the grid when pagination is enabled. and you should make sure that this property is specified in your json output if you want the pagination to work correctly.
example json output that is produced by the above action
{"dataitems":[{"Id":"5","FirstName":"jhon Lalic","Email":"jhon@gmail.com"},{"Id":"6","FirstName":"Maureen Joesph","Email"
:"maureen@gmail.com"}],"totalItems":2}
this contains two records. and the totalItems property.
ExtJSGridFilterHelper is a helper class that harbours most of the logic related to implementing sorting/paging and filtering of data. i am not going to explain what the source code is doing, step by step. but you can always read the source
.
parsing of parameters sent by the grid…(for the full source code please look at the source of ExtJSGridFilterHelper)
parameters related to paging
- start
- limit
parameters related to the sorting
- sort => the name of the property to be sorted(as given in the client side(aka field name))
- dir => direction
if (sort != null) //do sorting
{
objectsList = query.OrderBy("it." + sort).ToList();
if (dir == "DESC") objectsList.Reverse();
}
else
{
objectsList = query.ToList();
}
parameters related to the filters
public Filter(int id, HttpRequestBase request)
{
Id = id;
Field = request.Params[string.Format("filter[{0}][field]", id)];
DataType = request.Params[string.Format("filter[{0}][data][type]", id)];
DataValue = request.Params[string.Format("filter[{0}][data][value]", id)];
Datacomparison = request.Params[string.Format("filter[{0}][data][comparison]", id)];
}
how filters are processed, extract from the getExpression() method
</pre>
<pre>public FilterExpressionResult getExpression()
{
string expressionString = null;
var expressionParams = new List(); //paramerters collection
switch (DataType)
{
case "string":
expressionString = string.Format("(it.{0} like '{1}%')", Field, DataValue);
break;
case "boolean":
expressionString = string.Format("(it.{0} = {1})", Field, (DataValue == "true") ? 1 : 0);
break;
case "numeric":
switch (Datacomparison)
{
case "gt":
Datacomparison = ">";
break;
case "lt":
Datacomparison = "";
break;
case "lt":
Datacomparison = "<";
break;
default:
Datacomparison = "=";
break;
}
expressionParams.Add(new ObjectParameter("Param" + Id, DateTime.Parse(DataValue)));
expressionString = string.Format("(it.{0} {2} {1})", Field, "@" + "Param" + Id, Datacomparison);
break;
case "list":
var split = DataValue.Split(new[] { ',' });
var r = new string[split.Length];
for (var i = 0; i < split.Length; i++)
{
r[i] = string.Format("(it.{0} = '{1}')", Field, split[i]);
}
expressionString = string.Format("({0})", string.Join("OR", r));
break;
}
<pre>
source of ExtJSGridFilterHelper
public class ExtJSGridFilterHelper
{
public static List GetResults(HttpRequestBase request, ObjectQuery query, out int totalOjectCount)
{
var sort = request.Params["sort"];
var dir = request.Params["dir"];
//get non null values
int intStart, intLimit;
if (!int.TryParse(request.Params["start"], out intStart)) intStart = 0;
if (!int.TryParse(request.Params["limit"], out intLimit)) intLimit = 10;
List objectsList;
totalOjectCount = query.Count();
var i = 0;
var exspressions = new List();
var parameters = new List();
while (true) //check for filters starting from 0, if one exists move to the next one(0 -> 1...), .
{
if (!Filter.checkExistence(i, request.Params)) break; //...if not stop looking further
var expression = new Filter(i, request).getExpression();
exspressions.Add(expression.Expression);
parameters.AddRange(expression.Parameters);
i++; //keep track of index
}
var exspression = string.Format("({0})", string.Join("AND", exspressions.ToArray()));
//build the final expression
if (exspression != "()") query = query.Where(exspression, parameters.ToArray()); //filter collection on the expression
if (sort != null) //do sorting
{
objectsList = query.OrderBy("it." + sort).ToList();
if (dir == "DESC") objectsList.Reverse();
}
else
{
objectsList = query.ToList();
}
if (intStart + intLimit > objectsList.Count)
intLimit = objectsList.Count - intStart; //make sure the range we select is valid
objectsList = objectsList.GetRange(intStart, intLimit);
return objectsList;
}
#region Nested type: Filter
public class Filter
{
public string Datacomparison { get; set; }
public string DataType { get; set; }
public string DataValue { get; set; }
public string Field { get; set; }
public int Id { get; set; }
public static bool checkExistence(int filterIndex, NameValueCollection @params)
{
return (@params[string.Format("filter[{0}][field]", filterIndex)] != null);
}
public Filter(int id, HttpRequestBase request)
{
Id = id;
Field = request.Params[string.Format("filter[{0}][field]", id)];
DataType = request.Params[string.Format("filter[{0}][data][type]", id)];
DataValue = request.Params[string.Format("filter[{0}][data][value]", id)];
Datacomparison = request.Params[string.Format("filter[{0}][data][comparison]", id)];
}
public FilterExpressionResult getExpression()
{
string expressionString = null;
var expressionParams = new List(); //paramerters collection
switch (DataType)
{
case "string":
expressionString = string.Format("(it.{0} like '{1}%')", Field, DataValue);
break;
case "boolean":
expressionString = string.Format("(it.{0} = {1})", Field, (DataValue == "true") ? 1 : 0);
break;
case "numeric":
switch (Datacomparison)
{
case "gt":
Datacomparison = ">";
break;
case "lt":
Datacomparison = "";
break;
case "lt":
Datacomparison = "<";
break;
default:
Datacomparison = "=";
break;
}
expressionParams.Add(new ObjectParameter("Param" + Id, DateTime.Parse(DataValue)));
expressionString = string.Format("(it.{0} {2} {1})", Field, "@" + "Param" + Id, Datacomparison);
break;
case "list":
var split = DataValue.Split(new[] { ',' });
var r = new string[split.Length];
for (var i = 0; i < split.Length; i++)
{
r[i] = string.Format("(it.{0} = '{1}')", Field, split[i]);
}
expressionString = string.Format("({0})", string.Join("OR", r));
break;
}
return expressionString != null
? new FilterExpressionResult { Expression = expressionString, Parameters = expressionParams }
: null;
}
#region Nested type: FilterExpressionResult
public class FilterExpressionResult
{
public string Expression { get; set; }
public List Parameters { get; set; }
}
#endregion
}
#endregion
}
ah! that’s it for now. i might post some helpers that can be used in your views when you want to render the grid. the advantage of using this would be, so you won’t need to repeat all that javascript code in each and every view that you want to display a grid in. hope this will be helpful to someone. cheers!

'simple google' gm script screenshot 1

'simple google' gm script screenshot 2
we all agree that most of the google web apps our there are simple. i love simple things too. in fact i am a firm ‘believer’ of the KISS philosophy. any ways getting in to the subject. google search have been very simple so far. and it still is very much simple. but i feel at times that it could be much more simpler that it is. i don’t need it’s menu bar, and i don’t want to see all those other links it have on its page…etc so with my new found love jQuery, i thought of making it a bit more simpler. i just removed(set style to display : none; so its there, but its hidden from the user’s view) all most every thing other than the google logo and the text box in the page from the users view. it was pretty straight forward actually. here the code.
// ==UserScript==
// @name simplegoogle
// @namespace thekindofme
// @include http://www.google.lk/
// @include http://www.google.lk/webhp*
// ==/UserScript==
//hide the whole document till GM runs letsJQuery()
document.body.style.display = "none";
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://code.jquery.com/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
// Check if jQuery's loaded
function GM_wait() {
if(typeof unsafeWindow.jQuery == 'undefined') {window.setTimeout(GM_wait,100);}
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();
// All your GM code must be inside this function
function letsJQuery() {
$("body > div").hide();
$("center > *").hide();
$("center > div > div").hide();
$("center > div").show().css("margin-top", "20px"); //the google logo
$("center > form").show();
$("center > form > table > tbody > tr > td").hide();
$("center > form > table > tbody > tr > td:eq(1)").show();
$("center > form > table > tbody > tr > td:eq(1) > input").hide();
var searchTextBox = $("center > form > table > tbody > tr > td:eq(1) > input:eq(1)").css("padding", "2px 6px 4px 6px").css("color", "#555555").css("background-color", "#E2FFE4").css("border", "#dddddd 2px solid").css("-moz-border-radius", "10px").css("margin-top", "15px");
$("body").show();
arrangesearchTextBoxLayout(searchTextBox);
$(window).resize(function(){
arrangesearchTextBoxLayout(searchTextBox);
});
searchTextBox.show().focus();
}
function arrangesearchTextBoxLayout(searchTextBox){
searchTextBox.css("width", ($(window).width() /2)).css("font-size",($(window).height() /22) + "px");
}
keep it simple stupid
The MVCContrib project(at http://www.codeplex.com/MVCContrib ) provides a nice Grid ‘control’(and a lot more of course…) for use with MVC framework. Currently it does not support some what magical generation of javascript. so that you can have a ajaxified grid control. but with jQuery its pretty easy to ajaxify it. Today i finished ajaxifiying the paging of the grid, with a little bit of jQuery. So this is how i did it. i am sure that there must be betters ways of doing it. but this is what i came up with
Structure
First of all for this to work you will need some ’structure’ in your app. let’s take a little example. if you want to display a list of People in your application using a grid. you will have a controller for people: PeopleController. this would have a two actions. one would be List. while the other one would be ListForGrid. List action would be a plain action that will just return a normal view: List.aspx. while ListForGrid would return a asp ‘control’ or a partial view: ListForGrid.ascx. ListForGrid.ascx would know how to render a grid. it will use the normal Grid helpers to render a grid that displays people. List.aspx will contain an empty div:grid_holder and some java script that would add the Ajax goodness.
How it works
when a user asks for the People/List. the List.aspx would be rendered. and upon the document.ready() it would call ListForGrid action via ajax and load the resulting html in to the page. ListForGrid uses the ListForGrid.ascx to render the grid. ListForGrid action will also accept the necessary parameters to support Grid’s pagination(ListForGrid(int? page)).
this is all good. but still we have not archived our main goal. the rendered grid will have pagination links that would take us back to theListForGrid action directly. so if we click on them we would go to that resulting view with out any ajax ‘magic’. to get the grid to render them with ajax we are going to have to use jQuery to ‘hook’ in to the click event’s of the pagination related links( first | prev | next | last) that are in the rendered grid. and we will ‘intercept’ any events(clicks) and load the grid through ajax as necessary. so if you click on the next link in the grid we will stop the page from going to the relevant page, we will ‘read’ what ‘location’ that he is trying to take us to and we will load that through ajax.
now if you don’t understand what i am trying to tell you, please take a look at the source code bellow. i am sure you will understand that
relavant JS/jQuery source code
$gridHolder = "#grid_holder";
$ListForGridURL = "/People/ListForGrid";
//load the grid when the document loads. this code will run only once
$(document).ready(function() {
$($gridHolder).load($ListForGridURL, null, function() {
//now the grid is loaded call ajazPaging,
//this will hook on to the click events
ajaxPaging();
});
});
//hook on to the click events(in a recursive way) of all pagination
//related links, and when clicked load the updated grid through AJAX
function ajaxPaging() {
//this will zero in on the needed <a> elements in the Grid
$(".paginationRight > a")
.click(function(event) {
//stop the browser from going to the relevant URL
event.preventDefault();
//this.href will give us the href value of the current
//element, which have the URL from which we should update our grid
$($gridHolder).load(this.href, null, function() {
//call the function recursively so that the same code would
//run when the user click on the pagination links after the loading happens
ajaxPaging();
});
});
}
Update: with the release of jQuery 1.3 you can write this in a more simpler way using the live(). more info
Live Events
jQuery now supports “live events” – events that can be bound to all current – and future – elements. Using event delegation, and a seamless jQuery-style API, the result is both easy to use and very fast.
$gridHolder = "#grid_holder";
$ListForGridURL = "/People/ListForGrid";
//load the grid when the document loads. this code will run only once
$(document).ready(function() {
$($gridHolder).load($ListForGridURL, null, function() {
//now the grid is loaded call ajazPaging,
//this will hook on to the click events
//hook on to the click events of all pagination
//related links, and when clicked load the updated grid through AJAX
//this will zero in on the needed <a> elements in the Grid
$(".paginationRight > a")
.live("click", function(event) {
//stop the browser from going to the relevant URL
event.preventDefault();
//this.href will give us the href value of the current
//element, which have the URL from which we should update our grid
$($gridHolder).load(this.href);
});
});
});
rest of the sample source code(controller, actions and views…)
<em>PeopleController </em>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcContrib.Pagination;
using MvcContrib;
namespace MvcLocalization.Controllers
{
public class PeopleController : Controller
{
public ActionResult List()
{
return View();
}
public ActionResult ListForGrid(int? page)
{
ViewData["people"] = GetListOfPeopel().AsPagination(page ?? 1, 1);
return View("UserListGrid");
}
}
}
<em>
List.aspx</em>
<h2>People</h2>
<div id="grid_holder"></div>
$gridHolder = "#grid_holder";
$ListForGridURL = "/People/ListForGrid";
//load the grid when the document loads. this code will run only once
$(document).ready(function() {
$($gridHolder).load($ListForGridURL, null, function() {
//now the grid is loaded call ajazPaging, this will hook on to the click events
ajaxPaging();
});
});
//hook on to the click events(in a recursive way) of all pagination
related links, and when clicked load the updated grid through AJAX
function ajaxPaging() {
//this will zero in on the needed <a> elements in the Grid
$(".paginationRight > a")
.click(function(event) {
//stop the browser from going to the relevant URL
event.preventDefault();
//this.href will give us the href value of the current <a> element, which have the URL from which we should update our grid
$($gridHolder).load(this.href, null, function() {
//call the function recursively so that the same code would run when the user click on the pagination links after the loading happens
ajaxPaging();
});
});
}
<em>ListForGrid.ascx</em>
<%
Html.Grid(
"people",
column =>
{
column.For(p => p.Name);
column.For(p => p.Age);
column.For(p => p.Address);
}
);
%>
Aha! that’s it. jQuery rocks eh!
hope this will be useful to someone/myself.
Note 1: if you are completely new to MVCContrib Grid, you might want to read this
Note 2: a article on “Using MVCContrib Grid in a Web 2.0 World with jquery and AJAX” (btw this article does not cover implementing paging through ajax…)
I love OO for a reason. i don’t go around saying that i love OO so that everybody think i know a thing or two about software architecture. but because it have dividends. when you have a good design in your software, things become easy. some planning and some structure can make a lot of things very easy. if you follow OO principals you will have better and simple code. its very simple! you don’t follow OO principals, or any other principal(that have some acceptance, or that you knows to work well) you will end up with UGLY CODE! it might be easy to write ugly code. it might feel very ‘efficient’, ‘high performence’, ‘robust’ code at first. but in the end you will not have much performance gains(well may be about 0.001%, because you decided not to use setters and getters, to save method calls!!!). it will only make thing look bad an ugly. and that’s not all. i can assure you that your code will end up so complicated and full with ‘booby trap type bugs’ waiting to surface and explode.
believe me i know that gut feeling you get when you are writing ugly, unstructured code.
when you are writing stuff your mind will be ‘hot enough’ to handle all the complications that arise. so you will be able to wire up things, and put a few FLAGS! here are there and get things working smoothly. and you might feel smart too! but think for a minute. you are not supposed to write code to show how deep the ’stack of your brain’s cache’ is. it will be hard to understand in the future for your self. and what about others who look in to it? do you really think they want to read complicated code? it is not good to write complicated code. source code is supposed to be human friendly. you don’t need to become a machine. we have more than enough machine’s! think in a higher level people! we have enough processors/RAM and shit! brains are more precious than that stuff!
wow you want to write micro code? no i am not talking about short and straight forward code. i am talking about micro code! why would to want to write something in one line if it takes 2 minutes for someone to understand it, while the same thing could be written in 2 lines, making it easy for people to read. i mean sure it will be the same for the machine(in a given context). i think you should write code that is readable. making it too short while compromising human readability is bad.
every tiny nano/1000000000 second that you TRY to save for your compiler will cost the programmers 5 mins. think about it. is it worth it?
so now all of you(who are stupid and jobless to read this!
) know that this is a rant. now why i am ranting like this? well today i had to do some debugging in a project whose code was ‘tightly coupled’. yeah like really tight!!! the bug was in the UI layer, but i had to go thought the custom controller code(UI), UI code that uses the controllers, business logic… to understand how the “UI” works! the business class that was related to this incident had a property setter that was there just to support a certain feature in UI :O i am so shocked! this is so tightly coupled. but what can i do. i got to debug it anyways
huh… if you have to debug tightly coupled code my advice to you is… first of all claim down. and tell your self that sometimes you got to do things that you don’t like. and start debugging.
I’ve seen some best addons for firefox lists here and there in the web. and i thought i should roll out my own. cause… mmmm… oh well… these addons are too good to be missed! and btw i am using all these under ubuntu firefox 3.0.5 but i am sure that most of these will work on windows/OSX as well.
Ad Block Plus
the ever famous ad blocker. this saves me a lot of bandwidth. and at the same time this keeps web pages simple. when there are ‘no’ flashy ads, you can put your concentration on the real content that matters. however, do not block all the ads in all the sites. i mean the site owners should earn some profit too… i would leave google text ads from been blocked because for me they are ok. they don’t ‘disturb’ me. and they are informative.
NoScript
If you want to stay a step ahead when it comes to browser security, you need this. i have been using this addon for a while now. and by experience i can tell you that this gets updated really fast. and when configured correctly this will provide you solid protection without sacrificing the usability of the browser. when some new exploit comes out or when it appears in the wild, noscript will be the first to address it. i have seen noscript adding features to block some vulnerabilities in the browser even before mozilla devs rolls out a patch. so frankly if you are serious about security this is a must have.
Sxipper
it remembers all my passwords, usernames + how to fill in forms for me. i can maintain one or more profiles from which i can choose, when i am filling a form for a new account(signing up for some service online). people can ‘train’ it on filling forms in different web sites around the net. and then they can share it with other users. so most of the sites that i visit work correctly with sxipper(ie: sometimes when you have capitcha checks in a login page you just want sxipper to fill in the username and password, not auto submit the form)
DownThemAll!
built inside firefox, multi-threaded(ie download the same file divided in to several parts concurrently thus increasing the download speed) whats more can you ask for?
FireBug
web developers LOVE this one! this is basically a real time ‘IDE’(its not a IDE really
) for client side web development. you can try out so many things, debug stuff… try new things while instantly see the effects(ex: change the set of styles assosicated with some div elements and see the effects instantly!) with this addon. this is a MUST HAVE, if you do any serious web developing.
Greasemonkey
the web is yours!
you don’t need to wait till ‘they’ fix their ugly layout or till they decide to show up that piece of vital information just next to your profile image. you can write some scripts and get it done. this one addon makes the web a whole lot better than it is. sites like userscripts.org are filled with useful, creative scripts that makes your life easier.
iMacros
you have bash in *nix when you need some ‘glue’ to script repetitive actions. inside firefox you can use iMacros to script actions. its pretty easy to use and it can be really useful in some situations.
Linkification
how many links have you come across which are not really(none click-able URLs that appear as plain text) ‘links’? this addon will convert them in to links. it can be really useful at times. specially if you come across a lot of plain text URLs. this addon will save a lot of ‘copy, paste, click go’ actions, that you would have to do manually, otherwise.
Read it Later
sometimes you comes across nice stuff while browsing online. but you might not have enough time to read it all at that time. for ex: while you are at work you come across this cool article that you really want read but you don’t have enough time to do so, while you are still at work. you’d want to go home and read it, right?. Read it later can help you do exactly that. it can keep track of list of things, and sync them across multiple PCs.
FireFTP
firefox have minimum built in ftp capabilities. this addon add almost all the features that you would expect from a full featured stand alone ftp client. you can basically have a ftp client running in one of your browser tabs. now how cool is that?
btw: ftp is a inherently INSECURE protocol. so use it at your own risk
so those are addons that are of most importance to me. if you haven’t tried the above, why not give it a try and see, you never know what you have been missing
if you don’t want any of the installed addons you can simply uninstall them, cleanly after all they are jut addons.

niko bellic
“Insane Requirements ,Bad Graphics, Bugs, Lazy PC Port” – Well it was ok for me at least
well i my self didn’t experience that many problems with the game. and the graphics were ok for me. i am playing it with the highest settings possible(by setting them in the commandline.txt file… google gta iv commandline.txt if you want more info on this). and it works pretty neat on my box. i get 30+ frame rates and it looks alright to my eyes. it playable and i didn’t feel like it affected my gameplay. so i am happy as far as the graphics are concerned. but hey this does not mean that i didn’t have ANY bugs/problems… i had some instances where there were some textures that didn’t get rendered correctly. this one time all most all of the things around me turned black! and the only thing that i could identify was niko. but it happened only once so no big deal. there were other times where some shadows and some textures got rendered in a bit of a ‘buggy’ way. sometimes i could see through things that i shouldn’t be able to see through(ex:- shadows!). sometimes some textures got rendered with gaps in them(like if its rendering a piece of paper, i could see dots in that were transparent..).
btw i played the game on a PC, whos config is as follows…
Quad Core 2.4
4GB
Vista 64
8800GTS
2 SATA II drives configured with RAID 0
Using Xtreme-G FW 180.84 modded nividia drivers
“I Love the Story Man”
this been GTA, it had a great story line. i really enjoy the story in any game. its really important(for me at least). GTA IV has a good story. i am sure they could have made a movie out of it as well. seriously it would be grate if they do it right!. the main character in this version is Niko. he is from eastern europe, fresh off the boat. i don’t who creates all these story lines but whoever he is, he is doing a great job. i have played three versions of GTA. Vice City, San Andreas and now Liberty City. all of the had a good story line specially the last two titles. in SA the story was about some guy from the ‘ghetto’, coming out from the prison and stuff.. and here in Liberty City, its about a (illegal)immigrant.
When i am playing the game in the story line mode. i always try to listen to the stuff the characters say. the story keep me glued to all the dialogs. for me its like watching a movie. its even more exciting because you actually get to play though it. cool… and in IV you have this nice option, so when you are playing the single player missions you can sometimes in some missions make decisions! and they affect the story line. wow i should say that this is extremely cool. i am a fan of this lovely game.
“The Streets are Breathing, they are Alive”
Now most of us will finish the single player missions pretty soon. but we sure want to keep playing the game. fear not i think you can keep playing this for a long long time. the moment you start off with the game you can see that the liberty city is actually a live city. there are so many things happening around you and so many people are there. they are talking and they are simply alive. the city have a lot of activities happening in it. and i think this is just grate. if you have read any of the reviews of the game online you are sure to hear about this. and i experienced it my self. i feel that Rockstar have done a grate job making the city look alive. it breathtaking.
“Lets Play Some Pool”
There are lots of other things than shooting people and steeling cars in GTA. you can play all sorts of small games like pool, darts, bowling(well there is a bug in this, i hope they will patch it soon)… etc these are games that you can play and enjoy. yes they are nothing big as the GTA it self but still they are pretty interesting stuff.i like the pool game my self.
apart from these games there are some more features that i like a lot. one of them been cabs. you can actually hire cabs and use them to travel to destinations with in the city in a very short time frame. this will cost you some money, but i think this is a grate feature. i mean i am totally addicted to using cabs to travel inside the city, whenever i can. its fast its cheap.
“Take Cover!”
unlike in the previous version liberty city stories have a covering system that comes in to play in fire fights. in my opinion this makes things a bit more realistic. which is good. i love this feature as well. some reviewers say that in xbox this feature makes things a bit too easy for the players. but i don’t think its the same with the PC. you have to time your shots correctly and find good cover..etc so it doesn’t make it easy. but i feel that the cover system makes fire fights in GTA a lot more realistic(i am not saying this is how it happens in the real world. to be frank i don’t know much about real world firefights!, other than the ones they show on movies
)
“Windows Live, WTF is that! get it off my system!”
installing this stuff is mandatory??? come’on i can’t believe this. this is like forcing us to install some ad-ware!
well this is not ad-ware but i think that users should have a choice between using windows live and using something else(or may be, not using any thing like that at all!). this is still my machine and i should be able to get the game working with out installing all this bundled software. i am sure the Rockstar people must have thought about this. i mean it must have been some kind of a deal between them and the Windows Live people. so far i am ok with Live games… i mean its not a major headache or anything like that. it does not interfere with the game play that much. i think GTA is using it for managing user profiles(that means the Live guys must have some APIs… web services too, may be.. the developer in me wants to play with these stuff
)
Live would have been ok after all. but then there is this Rockstar Social Club app. i really don’t like that one. first of all when i just want to play the game i don’t want to go through it. i play it in the offline mode with no accounts and all that. so i have click about 5 or 6 buttons to skip logging in, say no to the message boxes… etc. and i have to do it every time i want to play the game. this is a pain in the neck. i am aware that this app can add some features and values to the game play… but all i am saying is that the user should have the ability to skip it if he needs to. i feel this is too much things to go through. when you are just playing a game. and the Social Club app keep running even when i am not playing the game. what is this thing? i don’t need another process hanging in there with no purpose. so i have to kill it my self. and i have to do it each time i finish playing the game. that sucks.
“All in all…”
I think this is a fantastic game. i am pretty sure that this is going to win a lot of awards and stuff like that. if you like playing computer games, i think you should definitely give this a try. you might need to get a good rig to run this game with high settings. but don’t let that stop you from playing this grate game. ha… i am looking forward to spending some looooong hours of game play. GTA all the way!
when i am using Netbeans to code i usually run it in my desktop because it is a bit resource hoggy(well i am saying this while jugging it by my standard’s. i only have 512 in my lap). i do this using NX. i have a NX server running in my desktop while my client has NX client. both machines are running Ubuntu(what else
). desktop is running 8.04 and the lap is running 8.10. so what am i writing all these in my blog? well i came across some problems while using the above setup. so i thought of noting them here.
Keyboard gone nuts: pressing the Arrow(↑) on the client(while logged in to the host via NX) brings up “Take Screenshot”
this is apparently a common issue. you would find plenty of materials on this if you google about it. for me the fix was setting the Keyboard model to Evdev Managed Keyboard in the Desktop(Host). you can go there through Preferences -> Keyboard preferences…
Screen fails to update/refresh it self as needed while on Netbeans, making it confusing to read the source code
this issue was a show stopper for me. i needed to code and with this i cannot. basically the issue is like this. when you are in netbeans writing some code, if you try to scroll an already opened text file, down and up again. the text get scrambled. basically some lines of code gets duplicated. for this to happen you should scroll the document down and then scroll it up again. this is only a display issue. so if you some how managed to refresh that ‘area of the screen’ you can see the code as it is.
after trying different options in the NX client. it seems that disabling the shared memory option in NX client’s display options do the trick. at least it works for me(in the login dialog click configure -> display setting tab -> display options -> disable shared memory).
i love netbeans. its pretty neat stuff. and NX is much better than VNC and stuff. so right now i can code using netbeans which is running in my desktop with a lot of resources, through my lap, which is in a very ‘physicaly’ reachable and comfortable place for me. in case if you are wondering why i can’t just use the desktop it self. well i can. but its not very comfortable due to bad furniture…. it hurts my hands. so that’s the reason i am using my lap.
How to switch from the full screen mode in NX client
simply take your mouse to the top right corner of your screen and click(the pixel in the top most right corner). cool ha? i found it through… google of course!
alright now i’m goning to sleep…

