Showing posts with label JSquared. Show all posts
Showing posts with label JSquared. Show all posts

10 August 2009

jQuery browser history manager

Despite being the creator of JSquared, I have recently had cause to use jQuery.

For the project where it is in use, I needed a browser history manager and after a very rapid cursory search, none came up that caught my eye. So, I decided to port my browser history manager from JSquared to jQuery and I woule like to make it available for you to use as well.

It works like all other browser history managers, appending named items with a value onto the hash portion of the URL in a QueryString like format.

The port is based on the new browser history manager that will be part of the next version of JSquared and it extends the jQuery object. You can download it here.

Using it could not be simpler. You set up named items you wish to listen for changes on and provide a callback function for when the change occurs. You can also update the value of an item as well.

To listen for changes, simply do the following:

$.url.listen( "myItemName", function(e, itemValue, itemName) {
    //perform some actions
});


Your callback function will be called when the user goes back and forwards and the value of your named item changes. The callback function will also get called if the user comes to your site from a bookmark and your item has a value.

To update the value of an item and create an entry in the users history, just do the following:

$.url.update( "myItemName", "myNewValue", false);


The third parameter to the update method is called "dontUpdateUrl". When set to true, the value shown to the user in the URL will not change, but the value internally within the browser history manager will. This is useful is you are updating many items and only wish to create one history point.

The new value can be any string and the object does not check that the length of the URL is safe - most browsers restrict URL's to around 2000 characters.

Please do feedback on whether this is working well for you.

14 May 2009

JSquared 2.1

I am very pleased to announce that JSquared 2.1 has been released as of this morning.

Check out the website for yourself, follow the JSquared blog, download the code and read the docs.

24 April 2009

Recent times

I am sure you will have noticed that posting has been light on here the last few weeks and I thought it might be interesting to talk a little about what I have been doing.

I have spent most of my spare time working on JSquared. The code base is coming on nicely now. I have completely re-written some parts of it even since the latest release and I am getting close to being code complete for the next version. Some more demos will come soon as well, but you can get more information on that over on the JSquared blog over the coming week or two.

Recently I have achieved the status of Certified Scrum Master. This is something about which I am very excited. I believe that Agile with Scrum is a fantastic engineering process. I have seen it in action myself whilst I worked at LBi and I am actively looking to assist Betfair in making the transition to this process. It can be painful and without doubt we are not yet getting it right, but with time, I am sure we will.

Now that I am officially certified, I feel more able to push things forward with Betfair and I have gained a great understanding of the process. I even think that I might bring some of the principles to my "normal" life, though I am not sure my wife will like a daily scrum meeting!

Hopefully I should be able to work on some more exciting things over the coming months than in the last few and I will be sure to blog about any and all the exciting happenings in the world of Betfair. For now, it seems I am far more an observer than participant in this great interface development world in which I live and work.

5 April 2009

JSquared 2.0

For those of you that might have missed the news, I released JSquared 2.0 today, April 5 2009.

This version represents a massive step forwards for the library. There is a whole new website to go along with the massively revamped code base. The documentation has become much more extensive and the website now features demos of how some of the library works.

The best thing to do is to check out the website for yourself, follow the JSquared blog, download the code and read the docs.

1 April 2009

CSS Selector Engines

CSS selectors are a great way to pick DOM nodes off a page and many JavaScript libraries use them extensively.

For those that have used JSquared, you will know that there is no CSS selector engine built in. Some people have asked why and I have always said that I think its simpler, faster, easier and makes a better performing application to use standard DOM methods to pick elements.

Well, yesterday I saw TaskSpeed and it got me thinking. I was about to start making a JSquared version of the tests in order to see what the performance was when I cam across this post which proved the point I wanted to make without me having to do any work!

Andrea shows that using native DOM methods is by far and away the fastest way to complete the tests. Whilst this isn't totally related to a CSS selector engine, it does show that native methods are the best and that certainly holds true for the few parts of the test where a selector engine would be used it it existed.

Of course, once all browsers in use support the Selectors API, all will be much improved,

20 January 2009

Browser detection

I am not a fan of user agent sniffing. I think it is a wholly inappropriate way to find out the capabilities of a user agent accessing your website.

However, it has its uses. Steve Souders built UA Profiler, for gathering browser performance characteristics. This is undoubtedly an important and intriguing project. In order to detect which browser you are using, he has written his own user agent sniffing code as he describes in his latest blog post.

He also offers to make this code available through a web service. This is an intriguing possibility. My first reaction was to think that this was an excellent move. A high quality user agent sniffer, improved upon by the community and its breadth of use that we could all access via a web service meaning we would not have to ask our users to download the code to do the detection.

Having calmed down slightly, I started to think more clearly. Overall, I am not convinced this is the best idea. Generally I feel that detecting a user agent from its user agent string is dangerous. This makes the assumption that no user agent spoofing is taking place, that your detection is granular and perfect enough and that browser features and support will not change. None of these are necessarily true.

I prefer to use feature detection. If I want to know whether the browser I am using is capable of applying a particular method, does that method exist?

The only reliable way I have found to detect a browser is for Internet Explorer only using conditional comments, both in HTML (useful for including CSS and JavaScript files for specific versions of IE) and in the JavaScript itself.

I notice that the latest version of jQuery (1.3) is deprecating its user agent sniffing. This decision is to be applauded.

I also realised that the next version of JSquared will actually add the ability to determine if the browser you are using is IE and if it is version 6 or another version. Whilst I might be adding a limited set of user agent support to my library, I am trying to do it in a fail-safe way.

7 January 2009

Happy new year

I wish you all a (slightly) belated happy new year.

2008 was a tough but amazing year. Some of its highlights for me included getting married, having an amazing honeymoon and having 3 different jobs!

Some of the less good parts included having 3 different jobs! I am working harder than ever now at home and at work and have had much less time in the last 6 months to blog or work on JSquared than I would have liked.

But things can change for 2009 and so I hope to only have 1 job this year, not get married again (as amazing as that was) and give a bit more time to blogging here.

As you can see on the JSquared blog, work has resumed on the project after a period of quiet and I have a new team member on board to make the project even better and an even bigger success.

This is hardly going to be a quiet year though as I hope to move house - here's hoping Mrs N and myself can find the right place!

I hope you all have a happy, healthy and successful year and keep reading....

17 September 2008

Another JavaScript pattern - private members shared between object instances

In this post, I will introduce another pattern to add to my previous post about my JavaScript patterns. In fact, this is an extension to those patterns and combines both patterns together! This is a singleton pattern that creates a constructor!

In this post I will also make use of some aspects of JSquared, so please refer to the JSquared website for more information on those.

So, private members shared between object instances. What does that mean? What it means is that I want to have some private variables and functions which are accessible to multiple instances of a constructor but without them being part of the constructor or the objects the constructor will create. A fairly well known way of achieving this is by making the prototype of my constructor an object built using something similar to my singleton pattern. This would allow public and private members on the prototype of my object. But that will not give me all I wish to achieve this time and it can be a clumsy syntax.

The code I will use to help explain this concept is designed to be a simple panel object. The object will manage a series of panels on a webpage which are linked. Only one panel can be open at a time. Typically one would construct this with a singleton object which finds all instances of the panels in the DOM and adds some handling to each DOM node accordingly. I will do this in a different way. This code example is of course merely a skeleton.

var Panel = new (function() {

 

    //add a DOM load event

    addLoadEvent( function() {

        //get all DIV elements with a class of "panel"

        document.getElementsByClassName( {cssClass: "panel", tags: "div", callback: function() {

            //create a new instance of the Panel constructor for each panel

            new Panel(this);

        } } );

    } );

 

    var panels = [];

 

    function closeAll() {

        for (var i = panels.length-1; i>=0; i--) {

            panels[i].close();

        }

    }

 

    //return the Panel constructor

    return function(panelNode) {

        this.open = function() {

            closeAll();

            //perform the open logic

        }

        this.close = function() {

            //perform the close logic

        }

        //add this instance to the all instances array inside the closure

        panels.push(this);

    }

});



Lets step through each part of this example and see what it does:


var Panel = new (function() {


Create a new variable called Panel using the singleton pattern.



    //add a DOM load event

    addLoadEvent( function() {

        //get all DIV elements with a class of "panel"

        document.getElementsByClassName( {cssClass: "panel", tags: "div", callback: function() {

            //create a new instance of the Panel constructor for each panel

            new Panel(this);

        } } );

    } );


Using JSquared methods add an event handler for when the documents loads. The handler will use another JSquared method to find all elements in the document which are DIVs with a class of panel and for each one run the supplied function which will create a new instance of Panel passing in the DIV node with the class panel that was found. (see the JSquared docs for more info on how getElementsByClassName is used)



    var panels = [];

 

    function closeAll() {

        for (var i = panels.length-1; i>=0; i--) {

            panels[i].close();

        }

    }


These are the private members which each instance of Panel will have access to. We have an array of panels which will get filled with each instance of Panel that is created and we have a closeAll method that loops through each instance of Panel and calls its close method.



    //return the Panel constructor

    return function(panelNode) {


We are going to return a constructor (using the standard constructor pattern). The variable Panel that we created at the top of the code example will now take the value of this constructor. In other words, Panel becomes a constructor which we can create instances of using the new keyword.



        this.open = function() {

            closeAll();

            //perform the open logic

        }

        this.close = function() {

            //perform the close logic

        }


Create open and close methods which will perform those actions. In the open method, we first want to close all the panels ensuring only one can be open at any time. To do that we call the private closeAll method which is available through the closure around Panel.



        //add this instance to the all instances array inside the closure

        panels.push(this);


Add this new instance (this line of code is still part of the Panel constructor) to the private panels array also available through the closure we have created.


To recap, we use the singleton pattern to execute some logic before returning a constructor which is then available to us later on in the page execution. We can use the closure this creates to make private members, declared inside the self executing function which is the singleton pattern. These private members are available to each instance of the constructor but the members are not available anywhere else within any JavaScript - as is usual for a closure of this type.

This can be a very powerful and useful pattern. When building a large application, I believe it is good to keep public members of all objects to a minimum and I also prefer not to use the prototype of an object unless I am using inheritance. This pattern achieves both of these aims in an elegant and encapsulated way.

12 July 2008

JSquared 1.1

It is with pleasure that earlier today I was able to announce the launch of JSquared 1.1. See the blog and the website for more details.

24 May 2008

JSquared 1.1 Beta 1

Just a quick note to point you to information regarding the release of JSquared 1.1 Beta 1.

11 May 2008

The JSquared blog

I am excited to announce the launch of the JSquared blog - http://blog.j-squared.info/.

From now on, this blog will discuss general web interface development issues whilst the JSquared blog will discuss features of JSquared and provide news of updates etc. You will see the latest few items in the left pane of this blog as well for convenience.

I have copied over the relevant posts to the JSquared blog. Please let me know if you have any comments.

8 May 2008

JSquared update

First of all, apologies for the sparsity of posts in the last few weeks. Its been a busy couple of weeks, but there is plenty to discuss. More of that to come in the next week or so.

The big news however is that JSquared has a new home - www.j-squared.info. There will be a major update to the website to coincide with the release of JSquared 1.1 in late June or early July. JSquared will have its own blog, freeing this blog for non-JSquared related topics generally. More on that in time to come.

An update on the progress of the goals for release 1.1:

JSquared Testing Platform
Using JSUnit, I have had much success testing JSquared. I have about 20% code coverage in the core library so far. My aim is still to have full coverage of the core library for version 1.1

Documentation
Using JSDocs, I have documented around 40% of the core library and I am getting close to a full API reference. This will be available along with a quick start guide for JSquared 1.1

FXSquared
The core FX library is complete. It is based on plugins and I am working on two plugins. I have been using the FX library for a few weeks now and it is nearly ready for release. It will be released with JSquared 1.1

IE 8 Beta Support
There has been some work on general compatibility updates and JSquared continues to improve. It is unlikely that JSquared 1.1 will be fully tested and working on any beta platform, but there will be compatibility updates to help with IE 8 and Firefox 3 support as well as improved Opera 9.5 support.

I hope you are as excited as I am by these updates and new developments. JSquared work continues at a good pace (despite protests from my wife) and I am hoping to increase the usage of the library over the remainder of the year.

Just to keep you all excited, Chris Heilmann gave a talk at AKQA last week and he has posted about it.

22 April 2008

Asynchronous JavaScript Part 5 - The JSquared AJAX Object

In my previous post in this series, I introduced some of my thoughts on AJAX. I will now go into detail as to how to use the JSquared AJAX object.

The AJAX object just like ADIJ is an instance based object. That is to say that for each AJAX request you wish to make, you create an instance of the object and then call a method on it to send the request. The same object can be reused or new instances can be created.

The AJAX object will require certain parameters to be provided and generally it is easiest to do so in the constructor, however, the object has methods for setting these values later.

Only one parameter is required and that is the URL the request is going to. The full list of parameters is:

URL - must be provided either in the constructor or using the setUrl method
method - the HTTP verb to be used for the request. Defaults to GET
onSuccess - the callback function if the request is successful
onFail - the callback function if the request fails
scope - the scope in which to run the onSuccess or onFail handler (the scope sets the value of this within the callback function). Defaults to the handler function itself
timeoutLength - the maximum time to wait for a server response to the request before timing out the request and calling the onFail handler. Defaults to 12 seconds
headers - an array of objects containing key and value pairs which are the additional headers to add to the request

Each of these parameters can be set once the object is created using a method.

To send the request, simply call the send method:

var myAjaxRequest = new J2.AJAX( {url: "myAJAXUrl.html"} );

myAjaxRequest.send();



The send method accepts an argument which is the string of data to add to the request. This argument is optional.

As you can clearly see, the parameters are passed into the constructor as a JavaScript object using object notation.

If you provide an onSuccess handler, that will be called if the request completes successfully and will have the AJAX object passed in as the first argument to the function. This will allow all the properties of the request to be accessed.

If the request should fail, the onFail handler will be called. The first argument passed to the fail handler is the AJAX object. The second argument will be the failure code. The code can then be examined by comparing it against the value of the AJAX failure codes object which looks as follows:

J2.AJAX.FailureCodes = {

    general: "xx1",

    unauthorised: 401,

    notFound: 404,

    timeout: 408,

    server: 500   

}



If you do not provide an onSuccess or onFail handler, then the object will simply do nothing - there will be no error shown.

Once again, I have to say that it is as simple as that. This is a really powerful but easy to use AJAX object.

This is the end of the series on Asynchronous JavaScript. I will be writing more about the other features of JSquared in the near future.

21 April 2008

JSquared road map and update

I have literally been swamped by a request for a road map for JSquared. In response, I have created this wiki page.

I have some ambitious plans for JSquared and it may not all be possible - much depends on how much my wife puts up with me working late into the night. But my aims are stated there.

I will be releasing periodic compatibility updates in between these major releases.

The single most requested feature for JSquared thus far is documentation and it is high on my priority list. I am intending on providing this as a set of object models output using JSDocs and an accompanying guide. Following that, I hope to be able to get a JSquared website up and running full of example code.

A set of unit tests and indeed a full testing platform for JSquared is also very high on the priority list and progress is being made on this. The intention for JSquared 1.1 is to get the core functions unit tested.

FXSquared is making good progress with a basic FX module ready for some heavy duty testing. FXSquared is built around plugins to allow for maximum flexibility.

IE 8 support continues to improve with each commit of the code. I expect IE 8 support to match that of all other browsers for JSquared 1.1

Please use this post or the wiki on the current JSquared home to comment on the road map, especially the form that the documentation should take.

7 April 2008

Documenting JSquared

One of the biggest missing aspects from JSquared is documentation.

I have been looking at using an automated tool to build documentation from comments in the code (another thing which is currently lacking) and this is going to be a major push for me over the next 2 months.

I have almost settled on using JSDoc. I would be very thankful if the readers of this blog could give any thoughts on JSDoc or any alternatives that I should be looking at.

Thanks in advance.

Safari 3.1 and IE8 Beta 1 support in JSquared

With the release of Safari 3.1 on PC and MAC, the handling of keyboard events has changed as detailed in this post on Ajaxian.

I am delighted to say that the auto filter object in JSquared 1.0 is fully compliant with Safari 3.1 and no changes to the code are required. This is the only object in JSquared with keyboard handling at present.

The big news recently though is the release of IE8 Beta 1. I am currently testing JSquared with this release and things are looking good. I expect there to be some minor changes to the code which will precipitate the release of a JSquared compatibility update. More news on this in the next few weeks.

24 March 2008

JSquared 1.0 released

Just a quick note to point you to JSquared 1.0 which is now in release. Please feel free to feedback on your thoughts.

18 February 2008

JSquared auto filter list

One of the updated objects released with JSquared 1.0 beta 2 is the auto filter object. This is a powerful object for filtering lists and is proving useful in a variety of circumstances, not least the creation of auto complete drop down lists for instance for searching.

At present the object only handles static lists, but by the time JSquared 1.0 is in final release, it will support live lists and a host of further options. That is to say that you would be able to provide a JSquared ADIJ or JSquared AJAX object as the list of values and the auto filter object will use that object to retrieve the list of values each time it needs to perform a filter.

The object is designed to work with a text input field and to respond to key presses to perform a filter. Within this structure, it is quite configurable. Lets have a look into the object and how to use it.

To initiate the object, create an instance using the "new" keyword. The constructor of the object takes 3 parameters, the third of which is optional:
  1. field - a reference to the DOM node which the auto filter object will respond to and work with
  2. data - the list of items to filter as an Array
  3. options - some options for how the object should perform
The options
  • objectMember - the array of data to filter can be an array of objects. If it is, the auto filter object needs to know which member on each object in the array contains the filter data. This option defines that.
  • minLength - the minimum number of characters to be entered into the text field before filtering starts. Defaults to 1.
  • hoverCssClass - the css class to add to the node which is hovered over (or selected) by the user once the filtered list is displayed
  • matchAnyCharacter - a boolean value which if true will match the characters entered into the text field against any character in the data list. If false (the default) it only matches from the start of the items in the data list

Events
In addition to these options, you can respond to various events. Pass in event handlers with the options as follows
  • onItemBound - fired when a matching item in the data list is found and bound to the document
  • onItemSelect - fired when the user selects an item from the filtered list
  • onFilteredListDraw - fired after the filtered list of items is added to the DOM tree
The first 2 events defined above run in the scope of the DOM node representing the relevant filtered list item. They both will get passed a parameter containing the original data for that list item. If the original data list was an array of objects, the whole object will get passed.

The third event defined above runs in the scope of the list of items.

The HTML
When the user enters a value into the field which is to support the filtered list, a list of matching items is added to the DOM immediately after the field. Assuming the value in the field is "value", this list will by default appear as follows:

<ul class="autoCompleteList">

<li><strong>value</strong> 1</li>

<li><strong>value</strong> 2</li>

<li><strong>value</strong> 3</li>

</ul>


This HTML can be styled by CSS. The css class defined for the currently hovered over item (or "hover" by default) is added to the appropriate list item.

Selecting an Item
When the user selects an item either by clicking on the item in the displayed filtered list or by using the keyboard (up and down cursor keys to choose an item and enter to select it), the value of that item is written into the field.

That is pretty much all there is to it. Its quite simple really!

JSquared Packages

With the release of JSquared beta 2, I have provided one file containing the full library which is also minified. This file weighs in at 29Kb! I would always recommend using JSquared in its componentized format, just including what you need at any time.

JSquared 1.0 Beta 2

JSquared 1.0 Beta 2 has just gone live and is available for download now.

This version has a number of enhancements over beta 1, not least the deployment method! You can now upgrade from beta 1 to beta 2 by just replacing the old code with the new. I have also provided full versions of the library with all its components in one file in both compressed and uncompressed formats.

There have been a number of improvements and additions since beta 1:

  • Better DOMReady handling
  • Much faster "trim" method on strings
  • New image rollover object - back to the future!
  • Delegate handling pattern
  • New auto filtering object. Highly configurable.
  • A URL manager object for adding entries to the browser history and allowing bookmarking in an AJAX application
  • Loads of little bug fixes
For those who still want it, beta 1 is still available for download as well.

Keep an eye on this blog for more information about all the JSquared features. You should already know about ADIJ!