28 May 2012

jquery - dynamic load of html fragments with script

Sometimes I want to load a fragment of HTML into the current page.

Often this is a blob of HTML that came in as part of a JSON object from AJAX. Also, I only might to extract part of the HTML that's returned - because for example sometimes the incoming fragment may be a whole partial view and I only want part of it.

( Note : this is similar to the jQuery $('#target').load('mypage.aspx #divToExtract') syntax )

Well, if you want to do it WITHOUT running script from the incoming fragment, do this:

                        var frag = $(jsonObj.HtmlFragment);
                        frag = frag.find('#divToExtract');
                        $('#target').html(frag.html());
But if you do want to run any script in the fragment, do this:
                        var frag = $('<div />');
                        frag.html( jsonObj.HtmlFragment );
                        frag = frag.find('#divToExtract');
                        $('#target').html(frag.html());

11 May 2012

Simple jQuery Navigation Menu


<script type="text/javascript">

    $(function () {
        $('.MyNavHolder .SubSection').hide();
        $('.MyNavHolder .Section').hover(function () { $(this).find('.SubSection').toggle('fast'); });
    });

</script>

<style>
    .MyNavHolder {background-color: #f00; overflow:visible; height: 30px; padding: 0px; position: relative;  }
    .MyNavHolder .MyNav {  position: absolute; right: 0; }
    .MyNavHolder a { color: #fff; text-decoration: none; display: block; }
    .MyNavHolder ul { margin: 0; padding: 0; list-style-type: none;}
    .MyNavHolder .Section { float: left;  background-color: Olive; width: 200px; cursor: pointer; }
    .MyNavHolder .Section .Header { height: 30px; border-left: 1px solid black;  }
    .MyNavHolder .Section .Header div { padding: 5px;  }
    .MyNavHolder .SubSection li { background-color: Fuchsia; padding: 5px; }
    .MyNavHolder .SubSection li:hover { background-color: Blue; }
</style>

<div class="MyNavHolder">
    <ul class="MyNav">
        <li class="Section">
            <div class="Header">
                <div><a href="">About Us</a></div>
            </div>
            <ul class="SubSection">
                <li><a href="">Contact Us</a></li>
                <li><a href="">History</a></li>
            </ul>
        </li>
        <li class="Section">
            <div class="Header">
                <div><a href="">McCormack</a></div>
            </div>
            <ul class="SubSection">
                <li><a href="">Account Settings</a></li>
                <li><a href="">Log Out</a></li>
            </ul>
        </li>
    <ul>
</div>

4 May 2012

Entity Framework 4 Set Default SQL Values On Save Changes

EF doesn't use the default values that you may have specified in your SQL Table schema.

So stuff like "DateCreated" fields that have getutcdate() as their default SQL value won't get set automatically when a record is saved from Entity Framework.

To solve that particular problem, I use the following approach. I enclose two styles, one for use with EF DbContext (POCO / CodeFirst), and one with EF ObjectContext (the default used in EDMX generation).

Note that you put this code within a partial extension of your EF Context class i.e. public partial class MyContext : DbContext or public partial class MyContext : ObjectContext

DbContext
public override int SaveChanges()
{
	this.ChangeTracker.DetectChanges();

	ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;

	List<ObjectStateEntry> objectStateEntryList = ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted)
																		.ToList();

	foreach (ObjectStateEntry entry in objectStateEntryList)
	{
		if (!entry.IsRelationship)
		{
			switch (entry.State)
			{
				case EntityState.Added:
					if (entry.Entity.HasProperty("DateCreated"))
						entry.Entity.GetType().GetProperty("DateCreated").SetValue(entry.Entity, DateTime.UtcNow, null);
					if (entry.Entity.HasProperty("DateModified"))
						entry.Entity.GetType().GetProperty("DateModified").SetValue(entry.Entity, DateTime.UtcNow, null);
					break;

				case EntityState.Modified:
					if (entry.Entity.HasProperty("DateModified"))
						entry.Entity.GetType().GetProperty("DateModified").SetValue(entry.Entity, DateTime.UtcNow, null);
					break;
			}
		}
	}

	return base.SaveChanges();
}
ObjectContext
public override int SaveChanges(SaveOptions options)
{
	this.DetectChanges();

	foreach (var insert in this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added))
	{
		if (insert.Entity.HasProperty("DateCreated"))
			insert.Entity.GetType().GetProperty("DateCreated").SetValue(insert.Entity, DateTime.UtcNow, null);
		if (insert.Entity.HasProperty("DateModified"))
			insert.Entity.GetType().GetProperty("DateModified").SetValue(insert.Entity, DateTime.UtcNow, null);
	}

	foreach (var update in this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified))
	{
		if (update.Entity.HasProperty("DateModified"))
			update.Entity.GetType().GetProperty("DateModified").SetValue(update.Entity, DateTime.UtcNow, null);
	}

	return base.SaveChanges(options);
}
If I helped you out today, you can buy me a beer below. Cheers!