Last active
August 29, 2015 13:59
-
-
Save lukefx/10735071 to your computer and use it in GitHub Desktop.
CustomDataSourceComboBoxRenderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.lugano.gisweb.controllers.edit { | |
import ch.lugano.gisweb.services.promise.Http; | |
import flash.events.Event; | |
import mx.collections.ArrayCollection; | |
import mx.core.IDataRenderer; | |
import mx.events.PropertyChangeEvent; | |
import mx.events.PropertyChangeEventKind; | |
import spark.components.ComboBox; | |
import spark.events.DropDownEvent; | |
public class CustomDataSourceComboBoxRenderer extends ComboBox implements IDataRenderer { | |
private var _endpoint:String; | |
private var filterFunction:Function; | |
private var _data:Object; | |
private var _valueField:String = "value"; | |
private var _required:Boolean = false; | |
public function CustomDataSourceComboBoxRenderer() { | |
super(); | |
width = 200; | |
addEventListener(DropDownEvent.CLOSE, this.closeHandler); | |
enabled = false; | |
} | |
private function closeHandler(event:Event) : void { | |
var pce:PropertyChangeEvent = null; | |
var newData:Object = null; | |
if (selectedItem == null) { | |
return; | |
} | |
if(selectedItem is String) { | |
newData = selectedItem; | |
} | |
else { | |
newData = selectedItem[_valueField]; | |
} | |
if(this._data != newData) { | |
pce = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE,true,false,PropertyChangeEventKind.UPDATE, "data", this._data,newData, this); | |
dispatchEvent(pce); | |
this._data = newData; | |
} | |
} | |
public function parse(json:Object):void { | |
dataProvider = new ArrayCollection(json as Array); | |
var provider:ArrayCollection = ArrayCollection(dataProvider); | |
if (filterFunction != null) { | |
provider.filterFunction = filterFunction; | |
provider.refresh(); | |
} | |
if (provider.length < 1) { | |
provider.filterFunction = null; | |
provider.refresh(); | |
} | |
// Riseleziono il valore | |
for each(var o:Object in dataProvider) { | |
if(o[_valueField] == _data){ | |
selectedItem = o; | |
break; | |
} | |
} | |
enabled = true; | |
} | |
public function get required() : Boolean { | |
return _required; | |
} | |
public function set required(value:Boolean) : void { | |
_required = value; | |
} | |
public function set data(value:Object) : void { | |
this._data = value; | |
} | |
public function get data() : Object { | |
return _data; | |
} | |
public function set valueField(value:String):void { | |
_valueField = value; | |
} | |
override public function set labelField(label:String):void { | |
super.labelField = label; | |
} | |
public function set filter(filter:Filter):void { | |
filter.getFilter().then(setFilterFunction).done(); | |
} | |
public function setFilterFunction(func:Function):void { | |
this.filterFunction = func; | |
} | |
public function get endpoint():String { | |
return _endpoint; | |
} | |
public function set endpoint(value:String):void { | |
_endpoint = value; | |
Http.get(endpoint).then(parse); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.lugano.gisweb.services.promise { | |
import com.codecatalyst.promise.Promise; | |
import com.esri.ags.utils.JSONUtil; | |
import mx.rpc.http.HTTPService; | |
public class Http { | |
public static function get(uri:String):Promise { | |
var service:HTTPService = new HTTPService(); | |
service.url = encodeURI(uri); | |
var header:Object = new Object(); | |
header["Content-Type"] = "application/json; charset=utf-8"; | |
header["Accept"] = "application/json; charset=utf-8"; | |
service.headers = header; | |
return Promise.when(service.send()).then(parseJson); | |
} | |
private static function parseJson(json:String):Object { | |
return JSONUtil.decode(json); | |
} | |
public static function getXml(uri:String):Promise { | |
var service:HTTPService = new HTTPService(); | |
service.url = encodeURI(uri); | |
var header:Object = new Object(); | |
header["Content-Type"] = "application/xml; charset=utf-8"; | |
service.headers = header; | |
service.resultFormat = "object"; | |
return Promise.when(service.send({ format: "xml" })); | |
} | |
public static function post(uri:String, params:Object):Promise { | |
var service:HTTPService = new HTTPService(); | |
service.url = uri; | |
service.method = "POST"; | |
var header:Object = new Object(); | |
header["Content-Type"] = "application/json; charset=utf-8"; | |
service.headers = header; | |
return Promise.when(service.send(params)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment