Just a quick post on something useful I came across today. In JavaScript you can access the stack trace via console using the following command (ctrl + shift + j):
console.trace()
In Chrome, this will let you navigate to each relevant call by clicking the line number in the console window. I’ve found it pretty useful for backtracking through jQuery exceptions.
Another quick one – how to stop a link from doing anything. I’ve just come across a need to use a link that acts as a button. A slight UI issue with this is that clicking a hyperlink will with an href value of ‘#’ will push scroll to the top of the page.
There are a couple of ways to go about fixing this. Note the href value in the following:
The second option is probably already familiar to anyone using ajax forms, simply have your function return false:
<a href='javascript:void(0)' onClick='myFunction()' id='my_awesome_link' data-comment_id="">Reply
function myFunction(){
//Do some awesome stuff
// ...
//Return false to prevent link from affecting anything
return false;
}
The third option is simply a jQuery-ish rehash of number 2:
/* Does some awesome stuff while preventing the calling link from directing the user */
$('#my_awesome_link').click(function(e) {
//Do some awesome stuff and then prevent the link from directing user
e.preventDefault();
});
Just a quick post on how to create a dropdown button using Twitter Bootstrap. It’s pretty straight forward, once you’ve included all your CSS etc just add the following code:
Once you save that you should see something similar to the image below: A standard twitter bootstrap dropdown button.
Change list item text alignment
To change the text alignment of this list items, all you need to do is adjust the text-align style assigned to the wrapper div.
Change dropdown button color
Once again, this is fairly simple, all you need to do is switch the button type. For instance, if you were after an orange button, you can swap btn-success with btn-warning. A list of the default styles is available on the Twitter Bootstrap site.
Ran into a bit of a problem with FullCalendar today after setting up a JSON feed. My events were only appearing in the month view. This StackOverflow post explains that this is due to the fact that the allDay property wasn’t set.
I’ve been mucking around with FullCalendar recently and decided to share one of the prototypes I’ve ended up with. It basically lets the user change the events without having to do a postback. A user simply has to click the event, type in the changes and hit update.
I’ve posted the code below, however there’s also a zip which is a little easier to manage. Note that you’ll probably want to clean it up a little if you plan to use it in production. The following libraries and plugins are also used:
– jQuery
– jQuery UI
– jQuery FullCalendar
– jQuery miniColors
What it Looks Like:
Full Calendar Example with Client Side Edits
How to Use It:
It’s all pretty straight forward, but just in case any one runs into issues there are two parts to this example. First, you generate an event template. You can then drag and drop this template onto the calendar as many times as you want.
The second part allows you to edit existing events without posting back to the server. To do this, simply click an event and then make the necessary adjustments using the top panel on the right. Once you’re done, just press update event.
The example uses the standard title property, but also includes a few others: descriptions, price, available. You can change/remove these to suit your needs, just remember to pull them out of the JavaScript as well.
Just a site that I came across today that was pretty useful for JSON: http://jsonlint.com/
I’ve been working with jQuery FullCalendar and needed some sample data. It has the option to use a JSON feed but mine wasn’t working for some reason. JSONLint allowed me to both format and validate it.
Let me know if you come across any other useful formatters/validators.
Thanks to this stackoverflow post I realised that the data wasn’t being serialised because I’d disabled most of the fields beforehand. This was done in order to prevent the user from changing the values. To get around this I simply had to serialise the data BEFORE disabling anything.
//Post via ajax
$.ajax({
type: 'POST',
url: 'uploads/add',
data: $(form).serialize(),
success: function(data, text_status, oHTTP){
handle_form_response(data, text_status, oHTTP, $(form).data('file_id'))
},
error: function(){
//Hide loader etc
set_form_loading(false);
//Unspecified error
alert('An error has occurred.');
},
dataType: 'json'
});
//Display loader and disable forms - DO THIS AFTER SERIALISING
disable_form_fields(true);