Putting It All Together
Now that you have separated the Model, View, and Controller layers logically, you can integrate them on the Web page. To write the contents to the screen, add the <script> for the news feed and a call to writeHeadlines() within a <div> element.
<div id="Web" class="WebFeed"
style="background-color: rgb(240,240,150);">
<!-- The src attribute below must be a single line -->
<script
src="http://p.moreover.com/cgi-
local/page?c=Web%20developer%20news&o=js"
language="JavaScript"></script>
<script language="JavaScript">
writeHeadlines(3,"Web Developer News");
Alternatively, you can integrate the Magportal Web Developer feed by using the URL obtained from MagPortal when you register for the feed on their Website.
Magportal numbers their Web feeds by category. Search their site for the proper category numbers.
You can output as many news feed blocks as you'd like to your pages by simply repeating the process. Modify the newsfeed.htm file from the sample code as shown in Listing 1 to display the Web Developer, XML, and Java news feeds:
I hope this simple example helped demonstrate the MVC design pattern. The most powerful aspect of this application is the fact that the writeHeadlines() function can be applied to ANY JavaScript news feed provided by moreover.com. All the news feeds have the same structure so it really is as simple as incorporating the feed and calling the function. If you want to incorporate other news feeds into your site (such as those provided by Magportal.com) you should examine the format provided and modify the writeHeadlines() method accordingly.
Tom Duffy , DevX's JavaScript Pro, is an Associate Professor at Norwalk Community College, Norwalk, CT where he teaches Web Development and Java. Tom is also the Manager of the NCC Ventures Lab, the College's in-house Web design studio. You can reach him via e-mail at: tduffy@ncc.commnet.edu.
Problem: Users like news feeds, and they can increase traffic to your site, but the real trick is to implement them and avoid creating a maintenance nightmare. How can you implement a newsfeed generically, with reusable code?
Solution: Use the Model-View-Controller (MVC) architecture to minimizes the changes required to switch news feeds. This solution shows you how to switch news feeds by changing just a few lines of code.
Do you use a newsfeed service now? Did you think you had to create complicated script or use controls to implement newsfeeds in your site? Do you like this article's approach to implementing a newsfeed? Join the discussions at Web.dhtml.general and Web.dhtml.scripting to get answers, make comments, or help others with their problems.