Blog posts


  • Repeat array in javascript

    August 30, 2011 | Tags: Javascript, Snippet
    This little snippet is useful if you want to extend an array in javascript with posts by repeating the content of the original array.

    For instance, your array contains "a", "b", "c". You wish to build an array of 8 posts repeating the initial values. This function would then return a new array containing "a", "b", "c", "a", "b", "c", "a", "b".
    <script type="text/javascript">
          

    function repeatArray(arr, count) {
    var ln = arr.length;
    var b = new Array();
    for(i=0; i<count; i++) {
    b.push(arr[i%ln]);
    }

    return b;
    }

    </script>
    And a little test bed:
    var a = new Array();
          

    a.push("test1");
    a.push("test2");
    a.push("test3");

    var b = repeatArray(a, 13);

    alert(b.length);

  • Use string.format in javascript

    August 30, 2011 | Tags: Javascript, Frax, Snippet
    This is a pretty neat snippet that attaches a format function to the string object. Works with infinit number of arguments: mystring.format('{0} and {1} and {2} and etc..', 'one', 'two', 'three');
    <script type="text/javascript">
          
    String.prototype.format = function() {
    var formatted = this;
    for (i=0;i<arguments.length;i++) {
    formatted = formatted.replace("{" + i + "}", arguments[i]);
    }
    return formatted;
    };

    alert('{0} is {1}!'.format('this','awesome'));
    </script>

    Code provided by my college Axtelius.

  • JQuery UI dialog ASP.NET postback problem

    August 24, 2011 | Tags: Javascript, ASP.NET, Troubleshooting, jQuery, Snippet
    I you're trying to use the jQuery UI dialog function together with ASP.NET form postback you might be in for a suprise. Most likely that suprise is that the fields will be returned empty to your server side code. And that would be a rather big problem.

    The reason for this is that the dialog function pulls your element and puts it in a window container which are placed outside the Form tag. And therefor if you do a postback directly from the dialog the input fields in the dialog are not included in the data that is posted to the server.

    But there's an easy way to solve this. And that is to add a line to your javascript code that move the dialog window back into the Frame element.

    var myDialog = jQuery("#myDialog").dialog({ width: 480, height: 400 });
          

    // This is where the magic happens :)
    myDialog.parent().appendTo(jQuery("form:first"));

    Or if you're more into one liners, here you go:
    jQuery("#myDialog").dialog({ width: 480, height: 400 }).parent().appendTo(jQuery("form:first"));
          

  • Randomize a page data collection in EPiServer

    August 24, 2011 | Tags: EPiServer, ASP.NET, C#, Snippet
    Here's a neat little function that will randomize the order of a PageDataCollection. The random order will be changed on each and every call (using a different random seed based on current date and time in ticks). If you're using the result in a paged list you might want to consider tweaking the code somewhat, either caching the list or by caching the seed and send it into the function instead of using the DateTime.Now.Ticks on every call.
    public static PageDataCollection Shuffle(PageDataCollection originalCollection) {
          
    // Create randomizer
    Random rnd = new Random((int)DateTime.Now.Ticks);

    // Create an array of PageData from the PageDataCollection sent to this function
    PageData[] pages = new PageData[originalCollection.Count];
    originalCollection.CopyTo(pages, 0);

    // Create a byte array of the same length and fill with random values
    byte[] randomBytes = new byte[pages.Length];
    rnd.NextBytes(randomBytes);

    // Sort array of PageData based on random values in the byte array
    Array.Sort(randomBytes, pages);

    // Return the randomly sorted PageDataCollection
    return new PageDataCollection(pages);
    }

  • 3 must have tools for .net developers

    April 30, 2011 | Tags: ASP.NET, C#, Regular expressions

    ReSharper

    ReSharper from Jetbrain is one of the most useful development tools I've ever come across. As a plugin to Visual Studio it offers code analysis - and suggesting changes to improve the code quality, smart refactoring and much more. You'll find a full list of features here.

    Download ReSharper (30 day trial)


    FxCop

    FxCop from Microsoft is also a great piece of software to increase the build quality of your project. By analyzing your assemblies (DLLs) against a best-practice set of rules it can pin-point problems in your code and offer suggestions.

    The installation is a bit daunting since it require you to download the complete Windows SDK. But it's worth it!

    Information on how to download FxCop


    Regulazy

    Constructing regular expressions can sometimes be really tricky. Wouldn't it be great to have a tool that helps you create a regular expression from a given string and then tweak it? Well that's what Regulazy does.

    Download Regulazy