Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Tuesday, October 11, 2011

A Vague JQuery Template Error

I have been getting an error in JQuery Template, and it's one of those errors that is impossible to debug. It's an issue that can only be fixed by intricately checking your code line by line, character by character. Here is the vague error that I'm getting:
Uncaught SyntaxError: Unexpected token return
Actually, here is the full "stack trace" (if that's what you want to call it) of the error:



I get no js compiler errors or anything, I just get this javascript error when I click on a radio button, but nothing more. My page loads correctly with no problem otherwise. Because of this fact, I was forced to inspect my code over and over and over and over... I removed code, I added code. I reworked my html a few times as well, but still got the error every time. I was literally going in circles trying everything.

The problem? I had an extra jQuery template tag in the very bottom of my script like this:

{{/if}}

At one point I tried combining both states into a single template using {{if}} {{else}} {{/if}}, but then gave up on that idea... Clearly I missed the closing tag and then never bothered to look at the bottom of my script tag when things didn't work. I'ts kind of insane that so much time was wasted on such a silly oversight on my part... Oh the whoa's of a programmer. Back to the grind.

Tuesday, July 28, 2009

Serializing Objects to Json in ASP.NET MVC

I've been working like crazy lately, and haven't been doing much blogging at all (reading or writing). I have a slew of blogs that I want to catch up on. I've barely checked my twitter lately, but reading my blog roll has completely fallen to the way side. The good news is that I've been learning a lot of jQuery, ASP.NET MVC, Linq, and overall enterprise application architecture in a very short period of time! This means that I have a brain full of geekiness just dying to get out.

Building this type of application is really a totally new type of programming for me and has really been putting my brain (and whiteboard) through the some long nights. Over the last few weeks I've really been able to see it all come to light. I'm shooting at a moving target (so to say) and there are changes that pop up along the way that change the system at it's core, where I'm forced to change how objects are constructed and so forth. While I could go on and on about the application I'm working on and all of the problems I've been faced with recently, the topic for this post is strictly related to how easy it is to serialize server side objects to the client.

Problem
I needed a way to represent server side memory objects as client side javascript objects. I have a set of div tags that need to be represented on a canvas (another div) and the user needs to be able to interact with them. Most importantly, I didn't want to re-invent the wheel entirely in javascript by creating all new classes and so forth.

Solution
Json.

Example
Let's say I have an object called "Person" that has a few properties and a collection of PhoneNumbers.

Person
  • Name
  • Address
  • City
  • State
  • Zip
  • PhoneNumbers[]
PhoneNumber
  • Type
  • Number
Now to the meat! In order to serialize these objects to Json, simply create a nested class (ie. a class that is defined within the parent class) that inherits from JavaScriptConverter:

class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public IList PhoneNumber { get; }

public class JsonSerializer : JavaScriptConverter
{
// override SupportedTypes, Serialize(), & Deserialize()
}
}

class PhoneNumber
{
public string Type { get; set; }
public string Number { get; set; }

public class JsonSerializer : JavaScriptConverter
{
// override SupportedTypes, Serialize(), & Deserialize()
}
}

Inside the 'SupportedType' member, simply return a new enumeration of the parent type. For example the SupportedTypes member inside Person.JsonSerializer would look like this:

get { return new[] { typeof( Person ) }; }

Inside the 'Serialize' method, simply cast the obj parameter to your type, and then begin setting each property like so:

var result = new Dictionary();
var person = obj as Person;
if (person == null)
return result;

result["Name"] = person.Name;
result["Age"] = person.Age;
result["Address"] = person.Address;
result["City"] = person.City;
result["State"] = person. State;
result["Zip"] = person.Zip;
result["PhoneNumbers"] = person.PhoneNumbers;

return result;

Inside 'Deserialize' do the same thing, only backwards (not covered here)... In order to take advantage of the Json data, let's initialize a PersonViewModel class. This class will be used by our View pages. Here is a quick example of what this class might look like:

class PersonViewModel
{
public PersonViewModel(Person person)
{
Person = person;
Json = string.Empty;
}

public Person Person { get; private set; }
public string Json { get; set; }

}

This class will be initialized by an Action method on our Controller class. Inside that method is where I initialize the Json property. In order to serialize our data to Json, we need to register our custom JavaScriptConverters with an instance of the JavaScriptSerializer. Let's do that now!

User calls:
http://mywebsite/Person/Edit/123

This will call the 'Edit' action on the PersonController; this will be where all of the things glue together.

public ActionResult Edit(int personID)
{
var person = _repository.GetPerson(personID);

var converters = new List();
converters.Add(new Person.JsonSerializer());
converters.Add(new PhoneNumber.JsonSerializer());

var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(converters);

var viewModel = new PersonViewModel(person);
viewModel.Json = serializer.Serialize(person);

return View(viewModel);

}

Inside the Edit view, we can now simply use a server tag to initialize a variable to be used by the page (in javascript notation). Here is the best part!!

var person = <%= Model.Json %>;

$(document).ready(function(){
alert(person.Name + ' is ' + person.Age + ' years old');
});

Cool huh?

Wednesday, December 17, 2008

jQuery

My eyes have been opened!

I've heard a lot about jQuery in the last few years, but was too stubborn to try and figure it out.

I thought it was not for me; I barely did any web development.
I thought it was something that only pertained to elite web developers.
I thought it was probably too complex to learn... let alone use.
I thought it was just a fad.
I thought way too much.
I thought wrong!

Tonight I was chatting with a buddy of Skype and asked him how to get around a problem I was facing in my web app. He turned me onto the jQuery site and praised how useful it was. Of course, my immediate reaction was a cringed and I thought "damn, now I have to try and learn this thing".

Man, was I wrong; there isn't much to learn at all! I simply went through the 5 minute tutorial on how it works and I'm up and running with a killer javascript API.
jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.
If you have ever used javascript (or html even), download this and add it to the top of your page html header section. Period. Thank me later...

Be sure to go through the tutorial: How jQuery Works - jQuery JavaScript Library

Thanks Ivan!