Posts

Showing posts from 2015

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&q

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 TypeNam

A Fun Bug in Visual Studio 2015 ASP.NET 5 Projects, And How To Fix It

I've been encountering a fun bug in an ASP.NET 5 project in Visual Studio 2015. From time to time, I get messages like the following when trying to compile the app: The type or namespace name 'AspNet' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) The fix for this is to simply remove one of the references from your project.json file, save the file, then re-add the reference and save the file again. All of that removing and re-adding causes the packages to get updated. A weird problem, but thankfully, a simple fix until a real one is implemented.

Unit Testing Angular When The $inject Property, Jasmine Spy Objects, and Promises Are Involved

I recently encountered an issue when writing a unit test for an Angular controller which was using the $inject property to specify its dependencies: even though I was using Jasmine to create mocks of the dependencies, the $inject property was overriding those and using the $provide service to instantiate new instances of the objects based on name. I went about setting up a beforeEach() call in the test spec to associate my mocks with the $provide service. However, I couldn't set up one of the mocks beforehand because it returns promises and I therefore needed to make use of the $q service to set up the mock. I tried to add an empty {} object to the $provide service, but this didn't work (I received different errors in the various stages of my work on this, from errors about a function being undefined, to one about there not being a constructor). What I had was a "chicken or the egg" situation: the $provide service registers providers for the $inject service to use,

How to Get ASP.NET 5 Apps To Run Under IIS

Recently I've been working on a web application using ASP.NET 5 and today had to deploy it to a web server in our test environment. Here are the steps I took to get this to work: Published the website project in Visual Studio 2015 by right-clicking on the project and selecting "Publish", I used the file system publish option, and specified the 64-bit version of the runtime we're using (in this case, 1.0.0-beta5). This is important, as I was unable to get IIS to host this website using the x86 version. I specified a path outside of the solution directory for the publish output. Inside the publish directory, the publish process created 2 folders -- approot and wwwroot, as well as a file called web (no extension), and web.cmd. On the web server, I created a new folder for the website. Inside this folder, I placed the output from the publish (the approot and wwroot folders and the two web files). I created a new website in IIS on the web server. For the app pool, I se