Posts

Using Underscore.js To Split an Array By Groups

Image
While working on a project today, I had to split an array of JSON objects into separate arrays based on one of the object's properties. Underscore.js makes this easy. In the example below, I'm splitting the sections  array into a new, multidimensional array called regions . var regions = _.toArray(_.groupBy(sections, function(section) {               return section.region;             })); Here's an example of the output logged to the browser's console:

Redirecting Assembly Versions in .NET

Image
As Kyle on South Park would say, "I learned something today." While working on one of our internal applications at work, I was getting the following exception: "System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) File name: 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'" But the funny thing was that all instances of this assembly in the solution were using version 1.3, and not  1.0 as the exception states. In fact, I could find no reference to version 1.0 anywhere in the config files. I enabled assembly bind failure logging (by setting the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1), and was given ...

How to Filter Out Objects With Empty Child Arrays When Using Angular's ngRepeat

Recently I was working on a website using Angular.js and needed to make use of the ngRepeat directive to iterate through a collection of objects. The UI in question needed to allow the user to select a child object, contained in an array, from one of the objects being displayed. And to provide a better user experience, I decided to only show those objects where the aforementioned array contained at least one object to select. Here's how I accomplished this using Angular's filtering capabilities, where elements  is the name of the property containing the child objects to choose from: <div ng-repeat="section in allSections | filter:{elements: []}:false"> Hopefully this will help someone trying to do something similar. :)

Beware JavaScript's Boolean() Function

I recently wrote the following line of code: newsletterPopupEnabled = Boolean($("#newsletterPopupEnabled").val()); The point of this was to correctly parse a string of "true" or "false", as well as to take into account an empty string (which I expected to be interpreted as false). I was very wrong. The Boolean() function does not parse a string, but simply tests the truthiness of the parameter passed to it. So, a string of "false" is interpreted as truthy because the string is not null. I now go to ponder the true meaning of "truth"... :)

Marvel '70s Style Packaging

Image
Found these at Target last week. Was surprised they used the classic '70s artwork for these characters rather than anything newer. I ended up buying these just because I liked the packaging -- but the granola bars are actually pretty good. :)

Angular Filter Negate Predicate Issue And Workaround

I recently ran into a funny quirk when attempting to specify a predicate negating condition on a filter in Angular.js. According to the Angular docs: The predicate can be negated by prefixing the string with ! . For example  { name :   "!M" }  predicate will return an array of items which have property name  not containing "M". First, here's what I was attempting to do: <div ng-repeat="section in newsletter.sections | filter: { sectionType: !1 } | orderBy:['region','order']"> In the example above, I'm attempting to filter out any sections of my newsletter object which have a sectionType other than 1. However, this wouldn't do the trick. The workaround was to append the exclamation mark as a string, as shown below: <div ng-repeat="section in newsletter.sections | filter: {sectionType: '!' + 1} | orderBy:['region','order']"> I guess when they said "prefixing the string...

Correct Model Binding for Derived Types in ASP.NET MVC 6

I ran into another fun problem today (why do so many of my blog posts contain that phrase?): I'm working on an ASP.NET 5 (MVC 6 / DNX) application, and I'd created a class that derived from another class. For example: (pseudo-code) public class Element public class HeaderElement : Element There's also a class which contains a generic list of the parent type: (pseudo-code) public class Section  { ... (other stuff) ...    public List Elements { get; set; } } Nothing unusual here, right? Well, I was sending that Section class shown above to client-side calls in JSON format, and everything was fine. However, when POSTing or PUTing back to my server-side controller, all instances of that derived HeaderElement class were instead deserialized as instances of plain old Element (the base class). It looked like I was going to have to write my own custom model binder for this... ...or so I thought. Turns out, there was a much simpler solution: setting the TypeN...