The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 12:34:23 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Watching for "dirty" elements in a web page... an observer pattern  (Read 2582 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: April 20, 2007, 09:38:12 PM »

This little class was modelled (but is not precisely) after the observer pattern. The notion here is that I often have lots of elements on a page that a user *might* edit... and I don't want to leave the page if the page is "dirty."

So, I instantiate one of these classes on a page, then register anything that might get edited by the user (and Im concerned about) with it. I also trap the leaving a page event and check this, so that if a user is editing some content on a web page, for example, and they browser elsewhere I can throw up a "Discard changes to this <whatever>?" confirm box and the ajax app looks more like a real application.

Here's the code - ping me if you need to see it in action:

Enjoy!
/p


Code:
// ----------------------------------------------------------- //
//                       dirtyElement                          //
// ----------------------------------------------------------- //
function dirtyElement() { this.clear(); }
dirtyElement.prototype.clear = function()
{
this.dirtyList = new Array();
this.dirtyIDX = new Array();
this.onChange = null;
this.isDirtyAny = false;
}
dirtyElement.prototype.clearDirty = function(theName)
{
this.dirtyList[theName] = false;
this.isDirtyAny = false;
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX[i];
if (this.dirtyList[thisName]) { this.isDirtyAny = true; }
}
if ( (!this.isDirtyAny) && (this.onChange) ) { this.onChange(false, theName); }
}
dirtyElement.prototype.clearDirtyAll = function()
{
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX[i];
this.dirtyList[thisName] = false;
}
if (this.onChange) { this.onChange(false); }
this.isDirtyAny = false;
}
dirtyElement.prototype.dirty = function(theName)
{
if ( (!this.isDirty(theName)) && (this.onChange)) { this.onChange(true, theName); }
this.dirtyList[theName] = true;
this.isDirtyAny = true;
}
dirtyElement.prototype.isDirty = function(theName)
{
return this.dirtyList[theName];
}
dirtyElement.prototype.proceed = function(theName, theMsg)
{
if (this.isDirty(theName)) { return confirm(theMsg); }
else return true;
}
dirtyElement.prototype.proceedAny = function(theMsg)
{
if (this.isDirtyAny)
{
ok2Go = confirm(theMsg);
if (ok2Go) { this.clearDirtyAll(); }
return ok2Go;
} else return true;
}
dirtyElement.prototype.watch = function(theName) {
this.dirtyList[theName] = false;
var ptr = this.dirtyIDX.length;
this.dirtyIDX[ptr] = theName;
}
// ----------------------------------------------------------- //
//                       dirtyElement                          //
// ----------------------------------------------------------- //
function dirtyElement() { this.clear(); }
dirtyElement.prototype.clear = function()
{
this.dirtyList = new Array();
this.dirtyIDX = new Array();
this.onChange = null;
this.isDirtyAny = false;
}
dirtyElement.prototype.clearDirty = function(theName)
{
this.dirtyList[theName] = false;
this.isDirtyAny = false;
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX[i];
if (this.dirtyList[thisName]) { this.isDirtyAny = true; }
}
if ( (!this.isDirtyAny) && (this.onChange) ) { this.onChange(false, theName); }
}
dirtyElement.prototype.clearDirtyAll = function()
{
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX[i];
this.dirtyList[thisName] = false;
}
if (this.onChange) { this.onChange(false); }
this.isDirtyAny = false;
}
dirtyElement.prototype.dirty = function(theName)
{
if ( (!this.isDirty(theName)) && (this.onChange)) { this.onChange(true, theName); }
this.dirtyList[theName] = true;
this.isDirtyAny = true;
}
dirtyElement.prototype.isDirty = function(theName)
{
return this.dirtyList[theName];
}
dirtyElement.prototype.proceed = function(theName, theMsg)
{
if (this.isDirty(theName)) { return confirm(theMsg); }
else return true;
}
dirtyElement.prototype.proceedAny = function(theMsg)
{
if (this.isDirtyAny)
{
ok2Go = confirm(theMsg);
if (ok2Go) { this.clearDirtyAll(); }
return ok2Go;
} else return true;
}
dirtyElement.prototype.watch = function(theName) {
this.dirtyList[theName] = false;
var ptr = this.dirtyIDX.length;
this.dirtyIDX[ptr] = theName;
}
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!