Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
ASP.NET MVC, JQuery and Cross-View Javascript Events

I wrote a nice little charting component for our site recently, and needed to call a Javascript function in one View (the one that was the main page for charting), from another View (one that went off to the server using Ajax to load up a list of feeds that could be charted).

This would have been traditionally done by putting a function on one View and calling it from another, but this is just a little bit "messy" and prone to breaking. Luckily I decided fairly early on to use JQuery on this site (mostly as I hate writing Javascript and JQuery makes it a doddle). With JQuery I can define a Javascript event across the two views, thereby providing me with a loosely coupled way of achieving my objective.

On my main index page I have:

$('#graphFeeds').bind("add", function(e, feedId, description){
        for(i=0;i<graphedFeeds.length;i++){
            if(feedId==graphedFeeds[i].feedId) return null; }
        var theFeed = new feed(feedId, description, "Green", "Line", 0, 1);
        $('#graphFeeds #table tbody').append(createFeedTableRow(theFeed));
        graphedFeeds.push(theFeed);
   });

And on the view that contains the list of feeds I have:

function addFeed(feedId, description) {
   $('#graphFeeds').trigger('add', [feedId, description]);}

Now by calling the addFeed() function on my list of feeds (I have a little "add to chart" link to the right of each feed), the Javascript event will be fired for the #graphFeeds div ... and the listening handler defined on the index view will go off on it's merry way and add the selected feed into the chart.

Much as I hate Javascript, I love JQuery 

 


Posted 09-17-2008 1:50 PM by Jak Charlton

[Advertisement]

Comments

ASP.NET MVC Archived Blog Posts, Page 1 wrote ASP.NET MVC Archived Blog Posts, Page 1
on 09-18-2008 11:25 PM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

Dew Drop - September 19, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - September 19, 2008 | Alvin Ashcraft's Morning Dew
on 09-19-2008 9:22 AM

Pingback from  Dew Drop - September 19, 2008 | Alvin Ashcraft's Morning Dew

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)