Cool~

I made an additional change that adds a microscopic delay. Otherwise IE 
fails to load TiddlyWiki for some reason :
-    dfd.resolve();
+    setTimeout(dfd.resolve, 0);

Only needed if changing the driver order to prefer TiddlySaver. I prefer the 
TiddlySaver driver because there are fewer security dialogs (see previous 
posts in thread).
Patch attached, including changes to enable TiddlyWiki in IE and Mozilla, as 
well. View the full twfile.js file at: http://jsbin.com/adehi5/3/edit

John

On Tuesday, May 10, 2011 5:12:46 AM UTC+9, Martin Budden wrote:
>
> This does indeed look like a good application of jQuery's Deferred 
> object. Also an excellent reason to upgrade TiddlyWiki to the latest 
> version of jQuery. 
>
> I'll get your patch plus the latest jQuery into the next alpha. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWikiDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/tiddlywikidev?hl=en.

--- jQuery.twFile.js	Sun May  8 13:56:34 2011
+++ twfile.js	Tue May 10 09:10:22 2011
@@ -19,14 +19,14 @@
 
 	$.extend($.twFile,{
 		currentDriver: null,
-		driverList: ["activeX", "mozilla", "tiddlySaver", "javaLiveConnect"],
+		driverList: ["tiddlySaver", "activeX","javaLiveConnect", "mozilla"],
 
 		// Loads the contents of a text file from the local file system
 		// filePath is the path to the file in these formats:
 		//    x:\path\path\path\filename - PC local file
 		//    \\server\share\path\path\path\filename - PC network file
 		//    /path/path/path/filename - Mac/Unix local file
-		// returns the text of the file, or null if the operation cannot be performed or false if there was an error
+		// returns the text of the file, or null if the operation cannot be performed or false if there was an error 
 		load: function(filePath) {
 			var d = this.getDriver();
 			return d ? d.loadFile(filePath) : null;
@@ -78,8 +78,22 @@
 			return localPath || originalPath;
 		},
 
-		// Private functions
+		// Deferred initialization for any drivers that need it
+		// returns a Deferred object so callback that executes as soon
+		// as twFile is ready can be attached
+		initialize: function() {
+			return $.Deferred(function(dfd) {
+				for(var t in drivers) {
+					if(drivers[t].deferredInit)
+						drivers[t].deferredInit();
+				}
+				// Kludge: give the <applet> some time to load
+				setTimeout(dfd.resolve, 0);
+			});
+		},
 
+		// Private functions
+		
 		// Returns a reference to the current driver
 		getDriver: function() {
 			if(this.currentDriver === null) {
@@ -92,12 +106,9 @@
 		}
 	});
 
-	// Deferred initialisation for any drivers that need it
+	// Automatically initialize on document.ready()
 	$(function() {
-		for(var t in drivers) {
-			if(drivers[t].deferredInit)
-				drivers[t].deferredInit();
-		}
+		$.twFile.initialize();
 	});
 
 	// Private driver implementations for each browser
@@ -239,12 +250,22 @@
 	drivers.tiddlySaver = {
 		name: "tiddlySaver",
 		deferredInit: function() {
-			if(!document.applets["TiddlySaver"] && !$.browser.mozilla && !$.browser.msie && document.location.toString().substr(0,5) == "file:") {
+			if(!document.applets["TiddlySaver"] && /* !$.browser.mozilla && !$.browser.msie && */ document.location.toString().substr(0,5) == "file:") {
 				$(document.body).append("<applet style='position:absolute;left:-1px' name='TiddlySaver' code='TiddlySaver.class' archive='TiddlySaver.jar' width='1'height='1'></applet>");
 			}
 		},
 		isAvailable: function() {
-			return !!document.applets["TiddlySaver"];
+			var isReady = false;
+
+			try {
+				isReady = !!document.applets["TiddlySaver"] &&
+						  ($.browser.msie || document.applets["TiddlySaver"].isActive) &&
+						  ( document.applets["TiddlySaver"].isActive() );
+			} catch (ex) {
+				isReady = false;
+			}
+
+			return isReady;
 		},
 		loadFile: function(filePath) {
 			var r;
@@ -311,3 +332,4 @@
 	}
 
 })(jQuery);
+

Reply via email to