My experiance with the prototype framework

by Jakobsson 15. January 2010 13:18

It has been a long time now since my last post. I have simply not found the time to write anything. But today I have a little spare time, so I tought I'd write a little bit about my experiance with the prototype javascript framework.

We have recently started a developer group in my home town, varberg, sweden. We have a meating every two weeks and we talk mostly about web development on the .net platform. Our last meating was yesterday where we disgussed different javascript frameworks. We decided to have a pair programming session where each pair was assigned a framework. Our task was to build a small photo gallery like this one.

I was assigned the prototype framework. I was pretty glad with this as I haven't tried it before and I wanted to test a more object oriented framework. I have mostly worked with jquery before wich is more about handling the DOM. We decided that we wanted to explore as much of the oo parts of the framework as possible. Even tough it was a pretty small task we decided to take the oo approch to the solution.

After working a bit with the framework I have to say that I'm very happy with it. I like the way they work with classes and I think it will help make the code more readable. It might not work to well in small web pages where there isn't much javascript. But in big application I think it realy shines. I will deffinatly look closer at the framework and try it out some more. And I really recomend it.

Tags:
Categories: Javascript

Using partial views for javascript

by Jakobsson 7. October 2009 17:56

Lately I have been working on a web application that requieres quite a bit of javascript. I decided to use the Mvc pattern here that I have written about before to see how it works out and to get a more readable and seperated javascript code. I did want to seperate the view, model and controller from each other in different files to be able to get a better overview of the javascript code. It can be very hard to read javascript if you have line after line in the view it self and to be able to reuse the same parts in different similar views.

I stared out by creating different .js files for each view, model and controller that i was using. The problem with this is that you will end up with a lot of different requests. On top of the libraries and plugins you use on the page, you will have three more .js files to include. Another dissadvantage is that you will not be able to use any server side method, like ResolveUrl() or anything on you'r model, while rendering the javascript.

After a while I decided to abandon the .js files and go for partial views instead. This way you will be able to seperate the javascript in different files, you will be able to use server side methods while rendering and you will get advantage of the intellisense support in visual studio (you will get this in .js files as well). While at the client side, all javascript will be embedded in the same view without making a lot of requests to the server.This way you get the best of both worlds in my opinion. The only dissadvantage I can see from using external .js files is when you are debugging. It can be harder to find the function you want to debugg and to jump between functions in the browser as all javascript is rendered at the same place. However, with search functions like the one in firebug, this shouldn't be a issue.

Categories: Javascript

Using the MVC pattern in javascript

by Jakobsson 20. August 2009 17:44

As anyone who have ever worked on a javascript heavy page knows, javascript can very easily get out of hand and become a mess. Roy Osherove thinks we should fix this problem by generating our javascript using a server side programing language. I didn't realy like this idea, nor did Jimmy Bogard. But the other day my friend, Magnus Bertilsson, showed me this nice article where Jonathan Snook talks about implementing the MVC pattern on the client side.

My first thought after reading the article was: "Why haven't I thought about this before?". It isn't realy a new idea, as there is a few frameworks out there already, but I have totaly missed that. It is pretty obvious, if we can use the pattern on the server side, why shouldn't we be able to do it on the client as well? To often you end up with a javascript method that respond to a event, ask the server for some data (ajax) and then displays it where it belongs. What if this could be separeted in 3 parts, one method to respond to the event, one that get the data you need and one to display it on the page? And all those methods will be seperated from each other in different objects. To me this seem like it can solve some of the maintainability issues you face while writing javascript.

I thought a little bit about how I would implement it. I decided I would have my controller and my model in two seperate .js files while I let the view sit in the html view. The reason I don't want the view in a seperate file is that it will only handle the html and I thought it would be easier to let it be as close to possible to the html. This gave me a file structure looking something like this:

My model will be responsible for getting all the data I need for the view. It can look something like this:

var Model = {
     getData: function(id) {
          [Ajax call to get data by id];
     }
}

Then I need a view to display this data. It would look something like this:

var View = {
     displayData: function(data) {
          [Edit the html in a desiered way with the new data];
     }
}

Then I just need the controller to get it all together. This could look something like this:

$(function() {
    $("#LoadDetails").click(Controller.details(1));
});

var Controller = {
     details: function(id) {
          var data = Model.getData(id);
          View.displayData(data);
     }
}

Here I handle the event binding in the Controller.js file. I could do it in the page as well, but for now I think its more logical to have it in the .js file as that is where the controller is that will handle the events. I'm thinking that each page (that need javascript on it) has one controller and one model. You can have multiple views to handle different things or parts of the page. I will have one model and one controller for the master page as well that all other pages can use. Here I can also have the methods that needs to be reused over more then one page.

This is what I'm thinking. I haven't acctually tried it out in any way yet. I will do that in my forum application when I get that far with it. I will think a little bit more about changes that can be made, and maby try out some existing framework as well.

It will, of course, not suite every application to structure your javascript in this way. Some pages don't have that much javascript and it's no problem to keep track of it anyway, and some developer teems might have some other pattern that suits them better. The good thing about the MVC pattern is that its nothing new, many developers allready know how it works and maby they are allready using it on the server side. Personaly I think it can solve a lot of my own problems.

Categories: Javascript

I would not abstract away my javascript

by Jakobsson 13. August 2009 13:30

Yesterday I saw this post by Roy Osherove where he talks about Script# and writing c# code to generate javascript code. I dont realy like this idé, as javascript and c# are two very different languages. While c# is a static programing language, javascript is a dynamic scripting language and it would be impossible to write everything with c# that you can in javascript.

Roy has some good points in that javascript has a way of getting out of hand and quickly become very unreadable. This is a problem that is hard to avoid when writing javascript heavy pages. Libraries like jQuery does fix this a bit and makes it a little bit more readable as you dont have to take different browsers into consideration as much. Of course c# will be a lot more readable still, but I think that the ability to use javascript as it was ment to be used, and use all of its features outweights the readability issue here.

Another thing you have to consider when writing a lot of javascript is exactly how its rendered. As it is sent to the browser it will effect the bandweight and you can save some time here by minifying the output.

The big problem with javascript development today is that we dont have many good tools to work with it. Visual studio is getting better with javasctip, but its still a long way to go. If we had some better tools, it would be a lot easier to develop javascript heavy websites and maintain them. Usually I'm a lot for abstractions of any kind, but abstracting away javascript, ni my opinion, is wrong. It is way to different from c# or any other static language to realy justify a abstraction.

Tags:
Categories: Javascript

Keep your javascript away from my markup

by Jakobsson 12. June 2009 13:19

Today, many web applications use alot of javascript to create a nice experiance for the user. With libraries like jQuery it is very easy to create that little extra that gives you this "woow" feeling. I use alot of javascript as well and I think it´s pretty fun writing when using a library like jQuery. One problem, however, that I often see in applications is that the html markup is depenting on the javascript. If the javascript doesn´t work correctly, then the markup is screwed up.

In my opinion javascript should be there to extend existing markup, not creating it. I tend to use a ".js file per view" approach while developing. Where I first create my view and make sure it work the way it´s supposed to without javascript. I then add a js file for that view and change the behaviour of some functionality. For instance I could do a asyncronas post instead of a normal form post or I can display some data in a popup instead of in a new page. This way you make sure you have unobtrusive javascript code and the javascript code is seperated from your html. I tend not to declare my events (like OnClick) directly in the markup, I´d much rather declare it seperatly. You can do this very easily with jQuery, like this:

.......



<input type="text" id="TextBox" />

.......


<script type="text/javascript"> $("#TextBox").click(function() {       //Do something }); </script>

To me that looks alot better then this:

.......


<input type="text" id="TextBox" OnClick="CallFunction();" />

.......


<script type="text/javascript"> function CallFunction() {      //Do something } </script>

This way you can read your .js file and understand most of what it does without looking at the markup and the view will be a bit cleaner and contain only html markup as it should, in my opinion. I see javascript as a layer above the html that you use to tweek the markup in a desirable way. I like to keep javascript in a file of it´s own, but that is just a personal preferance and there is nothing wrong with puting it in the view, as long as it is in the bottom where it belongs.

Tags:
Categories: Javascript

Javascript in my usercontrols at the bottom please

by Magnus Bertilsson 24. March 2009 16:22

So the other day I sat down to optimize my latest part of a page that I been working on and I realized that I had some javascript in a usercontrol. In the javascript I called .net functions like ResolveUrl and i didn't want the cost of yet another request with the thoughts of all my jquery plugins.

So what was my solution, well after a couple of bad ones I found a very smart one if I may say so myself :) I used one of my loves HtmlTextWriter and RenderControl.

 UserControl:


<asp:placeholder id="phr" runat="server">

<script type="text/javascript">......</script>

</asp:placeholder>

So I put my javascript in a placeholder in my usercontrol.

UserControl Codebehind:

protected void Page_Load(......) {

    var key = this.ID + "_script";
    var stream = new StringWriter();
    phr.RenderControl(new HtmlTextWriter(stream));
    phr.Visible = false;

    if (!Page.ClientScript.IsStartupScriptRegistered(key))
        Page.ClientScript.RegisterStartupScript(Page.GetType(), key, stream.ToString().Replace("\n", "").Replace("\r", "").Replace("\t", ""));

}

And here i render my placeholder to a string then hides it so it won't render at the usercontrols place on the page. Then i use the scriptmanager in a webform page, but you can build this in mvc / asp.net(without webforms) to without a scriptmanager and you minified the script with replace ;) This way you follow the yahoo tip to render javascript at the bottom of the page and in only one request.

I hope someone out there have use of this :)

Tags:
Categories: C# | Javascript

Extended jMonthCalendar with drag n drop

by Jakobsson 24. March 2009 12:54

For a few weeks me and Magnus (the other guy that is blogging here) have been working on a intranet. Mostly for learning purposes. The other week we needed a calendar to let users have a overlook at their schedule. We started googling a bit and found a great jquery plugin called jMonthCalendar. This was exacly what we needed.

Its a very easy to use plugin with many nice features. But one thing that we missed was the ability to move events around, and to have a easy way of adding new events to the calendar. I decided to take a look at the source and extend it. I added a option for draggable events and a callback for when a event is dropped. I allso added a callback for when a date is dubble clicked, because then we would open a window to add a new event at this date.

I use the drag n drop from jQuery UI, so if you like to take advantage of that functionality, you allso have to reference jQuery UI and ofcourse jQuery.

You can view a example of the extended plugin here. And you can download it with the source here.

The plugin was originally created by Kyle at www.bytecyclist.com/, so all creadit goes to him. Thank you for a nice plugin.

Categories: Javascript