Monday, April 23, 2012

Entity Framework and Saving Objects

I came across a new problem today with entity framework - In one of our projects at AIS, we use a repository to perform data manipulation. We have one function to get the user from the user id, something like:

// first open entities then
return entities.Users.SingleOrDefault(k => k.UserId == userId);

We also have another repository function to save that accepts a user as a param, opens entities, and calls a entities.SaveChanges() on the user. My first reaction was to get the user from the repository, make changes to the needed fields, and then pass the user to the save function.

Unfortunately, Entity Framework did not seem to like saving an existing entity object, and complained loudly, returning:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
It took me a while to find, but the solution seemed to be relatively easy - I just needed to detach the object after the initial selection:

// first open entities then
var result = entities.Users.SingleOrDefault(k => k.UserId == userId);

return result;

Eventually, this problem may be solved by refactoring the repository to use a single entities object, instead of creating the entity object in each call, but this works for now.

Friday, April 20, 2012

Internet Explorer 7-8 and KnockoutJS

Recently, I've been working on a project that requires IE7 minimum support and KnockoutJS. While Knockout supposedly supports IE6+,  it can be a little rough around the edges when it comes to the archaic Internet Explorers 7 and 8.

Here's a few of the hurdles I've come across so far:

IE 7&8 require quotes around reserved words, such as "class":

<div data-bind="attr: { 'class': cssclass }" >

IE 7 requires you use 'className' instead of 'class':

<div data-bind="attr: { 'class': cssclass, 'className': cssclass }" >

Not knockout specific, but IE7&8 do not support commas at the end of arrays [1,2,3,] - they will add an additional null element to the end of the array.

IE7 does not allow knockout comment bindings in <select> tags

IE7&8 may not support knockout's shorthand tags in some instances:

<div data-bind="..." />

IE7 does not include a native JSON.stringify that is used by ko.toJSON. You will need to include a helper shim.

Friday, April 6, 2012

Rearranging Images in

In one of my current projects, we require an image gallery that is basically sortable. The gallery we settled on, which is actually quite powerful, was Eventually,we ended up with a left and right arrow button under the gallery, which will modify the underlying data. After modifying the data we called .load() on the galleria gallery.

One problem - the gallery would load the new order of images, and then switch to the first image. Easy enough fix - we could just immediately call .show(). Unfortunately this created the problem of "jumping" images - .load would still start on the first image, and then quickly fly over to the current image, every time we reordered.

This seems to be a limitation of - loading does not allow us to specify a default start image. Our eventual fix. Unfortunately, was a two line change to

load : function( source, selector, config, index) {

  // Modified by AIS to allow index to be set at load time
  if (index !== undefined) = index;

We added a new param, index, to the load function, and set the start image as the index. Not pretty, but it worked. Anyone have a better idea?