The Model
For this application, the Model exists as a set of external JavaScript files. You can link to the script just as you'd link to any external JavaScript file:
<!-- 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>
The preceding script links to the Web Developer news feed. You can link to any JavaScript feed in the same manner. Use the URL provided by the feed provider for your selected newsfeed as the value of the src attribute for a <script> tag on your page.. The good news is that every script generated by moreover.com has the same structure. Each script includes a constructor for objects called "sa" objects (which I suspect stands for "script articles").
The sample code generates an sa object which has the properties listed above for each article in the news feed, and stores them in an array called article:
article = new Array(new sa
("http://c.moreover.com/click/here.pl?j35656245",
"Leaders Call for End to Mideast Strife",
"New York Times","text","Top stories"," ",
"http://www.nytimes.com","Apr 10, 2002 13:28",
" "," "),
//More articles here…
)
You can then access the individual articles by their index in the articles array. After retrieving an article, you can retrieve that article's properties by name.
For the record, the Magportal news feeds are structured similarly. An array of articles is created but the objects and properties are slightly different:
function i_57(mag,magurl,pubdate,
auth,title,desc,id,num_sim)
{
this.m = mag
this.mu = magurl
this.pd = pubdate
this.a = auth
this.t = title
this.d = desc
this.i = id
this.s = num_sim
}
a_57 = new Array(
new i_57("Macworld","http://www.macworld.com/",
"May 2002","Matthew Lowrie",
"Basic Package Simplifies the Creation of " +
"3-D Interactivity for the Web",
"Until now, successfully combining 3-D " +
"artistry and Web interactivity has " +
"challengedsoftware companies competing in " +
"the arena of 3-D on the Web...","95783","24"),
//More articles here...
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.