Jump to content


Suggestion of a new feature - Replace

Recommended Posts

Hello Jack, I have a suggestion to increase the loader (not sure if it exists), a hint on which I had problems and changed my code.


Imagine the situation, using the LoaderMax class to load an XML, and also loading the images automatically with the tag ImageLoader. Imagine we need to include a folder for the images in Flash itself, something like "../../stuff/ " and is necessary to include in the code, not in XML, how could we do?


If this still does not exist, I thought of something like a property that will replace words defined by the programmer, for example, in XML:


In case the LoaderMax perform a replace to change this text in XML, and include a path defined by the programmer.


In loaderMax, working with array, something like this:

loaderMax.append(new XMLLoader(new URLRequest("main.xml"), { name:"mainXML", replaceText:[{from:"[myPath]", to:"../../stuff"}] } ));


Let me know if you find it useful, if not, no problem.



Link to comment
Share on other sites

Not a bad idea, but this sounds a lot like the prependChildrenURLs() method and I'm trying to keep things nice and tight. The API would be a bit cumbersome for this sort of thing especially since it would really need to be able to be defined directly in the XML to make it super useful but two arguments would be required (the prependChildrenURLs() already works inside the XML, like ). But if you want to replace text in the URLs, you could do it manually without too much trouble, like:


var loaders:Array = myLoaderMax.getChildren(true, true);
var i:int = loaders.length;
while (--i > -1) {
   loaders[i].url = loaders[i].url.split("[myPath]").join("../../stuff/");


Did you see the prependChildrenURLs() method?

Link to comment
Share on other sites

Your solution is good to run the replace, but what if I need to load the XML, and within this same XML automatically load the images in what moment I use the code?


Once the XML is loaded (onComplete), I use the replace, and then the LoaderMax continue loading the images with the new path? How could we do?


Recalling that the problem is when we have the attribute load = "true" in XML.




Did you see the prependChildrenURLs() method?

But this is only in XML? If i need to use a variable? Like the path of the application in Zinc... like this: "mdm.Application.path".

Link to comment
Share on other sites

Alright, you talked me into it :)


I added a new replaceURLText() method to LoaderMax that should give you exactly what you're asking for. You can define stuff in XML too - just separate the fromText and the toText with a comma, like:


Link to comment
Share on other sites

Hi Jack!

Thank you for adding it, I believe it can help in some cases.

I did not know prependURLs, I thought it was only in the XML, I was wrong, sorry. In my case, "prependURLs" help me.

Anyway, I believe that "replace" will be useful.


But I could not make it work. Here's my code:


loader = new LoaderMax( { onComplete:completeHandler, onError:errorHandler } );
loader.replaceURLText("{path}", path); // or loader.replaceURLText("{path}", "../img/", true);
loader.insert(new XMLLoader(new URLRequest("_xml/main.xml"), { name:"mainXML" } ))
// or loader.replaceURLText("{path}", path);

The result:
Error #2032: Stream Error. URL: file:///D|/try/{path}myImg.png

Link to comment
Share on other sites

Did you replaceURLText() AFTER you inserted your loaders? As indicated in the docs, replaceURLText() only works on loaders that are in the LoaderMax instance when you call that function - it does NOT affect ones you add later.

Link to comment
Share on other sites

Yes, I tried every way to make sure, like:


loader = new LoaderMax( { onComplete:completeHandler, onError:errorHandler } );
loader.replaceURLText("{path}", "../img/" );
loader.insert(new XMLLoader(new URLRequest("_xml/main.xml"), { name:"mainXML" } ))
loader.replaceURLText("{path}", "../img/" );

Link to comment
Share on other sites

Are you trying to affect dynamic loaders that are created through XML? If so, you just forgot to set the "nested" parameter to true. By default, it only replaces text on the direct children of the container LoaderMax, but in your case, the urls are nested inside the XMLLoader (2 levels deep). So this should work:


loader.replaceURLText("{path}", "../img/", true);


But don't run that until AFTER your XMLLoader has fired its INIT or COMPLETE event - otherwise you're running the method before any of the loaders have been recognized. Also, if you have load="true" in your XML data, they'll begin loading immediately (right before INIT is fired), so if you want to replaceURLText(), you should either avoid setting load="true" and manually call load() on them after you've run replaceURLText() or just use the XML attribute to have XMLLoader automatically do it, like:



Make sure you've got the latest version too (just posted an update)

Link to comment
Share on other sites

  • 3 months later...

Hey Jack,


Really love LoaderMax so far, but I too was wondering about this feature :)

BulkLoader actually has a pretty interesting way of doing this. Here's some code I used on a previous project of mine:


var substitutions:Object = new Object();
substitutions.base = _model.base;
substitutions.language = _model.language;
substitutions.stream = Constants.WOWZA_BASE + Constants.WOWZA_OUTPUT;

var url:String = _model.base + ASSETS + "?" + RandomGenerator.generate(4);
_loader = new LazyXMLLoader(url, "nav-siteloader", 5, 10);
_loader.addEventListener("lazyComplete", lazyHandler); // listen for when the external definition finishes loading
_loader.addEventListener(BulkLoader.COMPLETE, completeHandler);
_loader.addEventListener(BulkLoader.PROGRESS, progressHandler);
_loader.addEventListener(BulkLoader.ERROR, errorHandler);			
_loader.stringSubstitutions = substitutions;
_loader.allowsAutoIDFromFileName = false;


What I like about this approach is that you can provide as many substitutions as you wish and you don't have to worry about when you do those substitutions. (I gathered from your response that I need to wait until the XMLLoader has fired the INIT event, and that I can't have my LoaderMax node's load attribute set to 'true')


Reasons why this would be useful:

- loading files dependent on language parameters (config_en.xml, config_fr.xml)

- no need to change the XML files when switching from development to live servers (only need to dynamically set the base - or whatever you want to call it - parameter to the site's base url once, through a flashvar or whichever way you prefer)


But it's just a suggestion, for now I'm quite pleased with the prependURL feature. :)


The XML file looks like this:


<?xml version="1.0" encoding="UTF-8"?>



Link to comment
Share on other sites

Are you familiar with LoaderMax's replaceURLText() feature?


queue.replaceURLText("{base}", "http://www.greensock.com/images/");
queue.replaceURLText("{language}", "_en");


That would give you what you need, right? The down side to the BulkLoader feature implementation the way you described it, special characters couldn't be used. Like substitutions.{language:english} = "en"; or whatever. And I kinda like being able to control when the substitution occurs.


Oh, and XMLLoader recognizes a replaceURLText attribute as well, so you can do all this stuff inside the XML too (as long as you nest your loaders inside a node of course).

Link to comment
Share on other sites

  • 2 weeks later...

I definitely like LoaderMax's replaceURLText() feature, and it seemed to do the trick, but I ran into an issue...

Since I can't set the load attribute on the LoaderMax node to true, the progress of the nested LoaderMax nodes doesn't get integrated into the XMLLoader's overall progress.

Any thoughts on how we could use replaceURLText, but still have integrateProgress work? I've been working through the documentation, but couldn't really find a solution so far.


For now, I've fixed it by adding LoaderEvent.COMPLETE listeners to each of the nested LoaderMax nodes (and the main XMLLoader), setting a boolean when each of them loads and then checking if all flags have been set to true before proceeding.

Link to comment
Share on other sites

You could simply listen for your XMLLoader's INIT event and get your nested LoaderMax stuff that was parsed from the XML and then append() that LoaderMax into your parent LoaderMax (the one that contained the XMLLoader). Kinda like:


var queue:LoaderMax = new LoaderMax({onProgress:progressHandler, onComplete:completeHandler});
queue.append( new XMLLoader("data.xml", {onInit:xmlInitHandler, estimatedBytes:50000}) );

function xmlInitHandler(event:LoaderEvent):void {
   var xmlLoaderMax:LoaderMax = LoaderMax.getLoader("myNestedLoaderMaxName");
   xmlLoaderMax.replaceURLText("{language}", "nl");
   queue.append( xmlLoaderMax );

Link to comment
Share on other sites

Huh. Very cool. :) I'll try that out. Thanks for the suggestion!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.