Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
old URLLoader and new UrlLoader — Gideros Forum

old URLLoader and new UrlLoader

atilimatilim Maintainer
edited March 2012 in General questions
Hi,

With the release of 2012.2.2, we've removed the URLLoader class and introduced a new one: UrlLoader. If your application already uses URLLoader and if you don't want to update your code (but please update it :) ), you can use this class which implements the same interface with the old URLLoader:
URLLoader = Core.class(EventDispatcher)
 
URLLoader.GET = "get"
URLLoader.POST = "post"
 
function URLLoader:init(url, variables)
	self.loader = UrlLoader.new()
	self.loader:addEventListener(Event.COMPLETE, self.onComplete, self)
	self.loader:addEventListener(Event.PROGRESS, self.onProgress, self)
	self.loader:addEventListener(Event.ERROR, self.onError, self)
 
	if url ~= nil then
		self:load(url, variables)
	end
end
 
function URLLoader:load(url, variables)
	local method = UrlLoader.GET
	local body = nil
 
	if variables ~= nil then
		if variables.method == URLLoader.GET then
			method = UrlLoader.GET
			if variables.data ~= nil then
				local get = {}
				for k,v in pairs(variables.data) do
					get[#get + 1] = k.."="..v
				end
				if #get ~= 0 then
					url = url.."?"..table.concat(get, "&")				
				end
			end			
		elseif variables.method == URLLoader.POST then
			method = UrlLoader.POST
			body = variables.data
		else
			error("Parameter 'method' must be one of the accepted values.")
		end
	end
 
	self.loader:load(url, method, body)
end
 
function URLLoader:close()
	self.loader:close()
end
 
function URLLoader:onComplete(event)
	self.data = event.data
	self:dispatchEvent(Event.new(Event.COMPLETE))
end
 
function URLLoader:onProgress(event)
	local event2 = Event.new(Event.PROGRESS)
	event2.bytesLoaded = event.bytesLoaded
	event2.bytesTotal = event.bytesTotal
	self:dispatchEvent(event2)
end
 
function URLLoader:onError(event)
	self:dispatchEvent(Event.new(Event.ERROR))
end
thanks

Edit: Interfaces of URLLoader and UrlLoader are very similar to each other. It'll be easy to update your code.

Likes: Janberka

Dislikes: mehmetuysal

+1 -1 (+1 / -1 )Share on Facebook
Sign In or Register to comment.