actionscript 3 - Handling promises when dealing with event listeners -
currently i'm using promises in as3
project relative js
projects.
say have code, in as3
:
public function getfeed():promise { var request:urlrequest = new urlrequest(end_point); var loader:urlloader = new urlloader(); var dfd:deferred = new deferred(); loader.addeventlistener(event.complete, handlegetfeedready, false, 0, true); loader.load(request); return dfd.promise; } private function handlegetfeedready(e:event):void { e.target.removeeventlistener(event.complete, handlegetfeedready); vo.entries = json.decode(e.target.data); // resolve dfd here }
what i'd somehow pass dfd
(deferred) object event handler. know could use anonymous function so:
loader.addeventlistener(event.complete, function(e:event):void { handlegetfeedready(dfd, e); }, false, 0, true);
but i'd steer clear of using anonymous functions created each time call parent method (maybe i'm being anal).
another choice create class reference dfd
object this'll create problems when want call method simultaneously.
so potential answer, not sure if it's correct method it's suiting problem , project now: extended urlloader
, added class variable relative loader holds dfd
object, so:
package com.domain { import com.codecatalyst.promise.deferred; import flash.net.urlloader; import flash.net.urlrequest; public class urldeferredloader extends urlloader { private var _dfd:deferred; public function urldeferredloader(dfd:deferred, request:urlrequest=null) { super(request); this._dfd = dfd; } public function dfd():deferred { return _dfd; } } }
it job quite nicely!
Comments
Post a Comment