该帖已经被评为良好帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-01-25
ltian 写道 JavaJason 写道 ltian 写道 JavaJason 写道 ltian 写道 看了最后的一个图,也就是你的UserVo采用lazy load方式获取DepartmentVO时候的代码,我觉得使用者使用这个类的时候可能并没有想象的那么美好。因为当DepartmentVO!=null时,调用方可以立即地,直接地得到这个对象。 而当DepartmentVO==null时,调用方还期望能够立即地、直接地得到这个对象,那恐怕要失望了。因为Flex访问后台服务的时候采用的是异步调用。
这一点我倒也曾经和你一样担心过这个问题,但Flex其实给我提供了一个data binding的功能,所以如果我们并不需要同步响应的时候,就让它异步好啦 当响应从Server端返回时,在它的result方法里改变页面component所binding的对象的值,Flex会帮你做自动的更新 Flex给我提供的data binding的功能确实好用,但是按照上面的实现,我们所提供的getXXXX方法API的语义就不稳定了,再一定程度上会造成使用者的混乱,可能需要加强团度的培训,以使所有成员都必须清楚地认识到这一点。 你这一点算是说到我心里去了 这也是我遇到的一个问题之一,也还没有再做深入的研究,有这方面的经验分享一下吗? 这是我的UserVO.as中的代码 [Bindable] public var departmentVO : DepartmentVO; //todo //why cannot use "get departmentVO()", and bind userVO.departmentVO.name to datagrid public function getDepartmentVO():DepartmentVO { var responder:LazyLoaderResponder; if(departmentVO == null && !isNaN(departmentId)) { responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); } return departmentVO; } 首先感谢你分享思路,说实话,我也刚开始这方面的工作,还没有深入研究,如果有好的实践一定与大家分享。 It is impossible to use "function getDepartmentVO():DepartmentVO " to get the return DepartmentVO Because you use Remote Object! Generally the Sychronize way you can use function getDepartmentVO():DepartmentVO { dosmothing; return DepartmentVO ; } But ASycronize way you cant do this it should be : [Bindable] var returnVO:DepartmentVO; function getDepartmentVO():void{ myservice.getVOByRemoterObject(); the RemoteObject.addListener("getDepartment", theDepartmentVOHandler); } function theDepartmentVOHandler(event Resultevent){ returnVO = event.casttoDepartmentVO; } it is impossible to getDepartmentVO() directly |
|
返回顶楼 | |
发表时间:2008-01-25
houwei 写道 ltian 写道 JavaJason 写道 ltian 写道 JavaJason 写道 ltian 写道 看了最后的一个图,也就是你的UserVo采用lazy load方式获取DepartmentVO时候的代码,我觉得使用者使用这个类的时候可能并没有想象的那么美好。因为当DepartmentVO!=null时,调用方可以立即地,直接地得到这个对象。 而当DepartmentVO==null时,调用方还期望能够立即地、直接地得到这个对象,那恐怕要失望了。因为Flex访问后台服务的时候采用的是异步调用。
这一点我倒也曾经和你一样担心过这个问题,但Flex其实给我提供了一个data binding的功能,所以如果我们并不需要同步响应的时候,就让它异步好啦 当响应从Server端返回时,在它的result方法里改变页面component所binding的对象的值,Flex会帮你做自动的更新 Flex给我提供的data binding的功能确实好用,但是按照上面的实现,我们所提供的getXXXX方法API的语义就不稳定了,再一定程度上会造成使用者的混乱,可能需要加强团度的培训,以使所有成员都必须清楚地认识到这一点。 你这一点算是说到我心里去了 这也是我遇到的一个问题之一,也还没有再做深入的研究,有这方面的经验分享一下吗? 这是我的UserVO.as中的代码 [Bindable] public var departmentVO : DepartmentVO; //todo //why cannot use "get departmentVO()", and bind userVO.departmentVO.name to datagrid public function getDepartmentVO():DepartmentVO { var responder:LazyLoaderResponder; if(departmentVO == null && !isNaN(departmentId)) { responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); } return departmentVO; } 首先感谢你分享思路,说实话,我也刚开始这方面的工作,还没有深入研究,如果有好的实践一定与大家分享。 It is impossible to use "function getDepartmentVO():DepartmentVO " to get the return DepartmentVO Because you use Remote Object! Generally the Sychronize way you can use function getDepartmentVO():DepartmentVO { dosmothing; return DepartmentVO ; } But ASycronize way you cant do this it should be : [Bindable] var returnVO:DepartmentVO; function getDepartmentVO():void{ myservice.getVOByRemoterObject(); the RemoteObject.addListener("getDepartment", theDepartmentVOHandler); } function theDepartmentVOHandler(event Resultevent){ returnVO = event.casttoDepartmentVO; } it is impossible to getDepartmentVO() directly Please take a look at following two lines responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); LazyLoaderResponder is the event handler It's the same solution you provide |
|
返回顶楼 | |
发表时间:2008-01-25
public function getDepartmentVO():DepartmentVO
{ var responder:LazyLoaderResponder; if(departmentVO == null && !isNaN(departmentId)) { responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); } return departmentVO; } But you still have a "return departmentVO" to Return a object from your function. This is null object if you call this method. it is impossible to RETURN departmentVO in Asyc function. That is my point. anyway your LazyLoaderResponder looks clean Could you post your LazyLoaderResponder ? Thanks |
|
返回顶楼 | |
发表时间:2008-01-28
houwei 写道 public function getDepartmentVO():DepartmentVO
{ var responder:LazyLoaderResponder; if(departmentVO == null && !isNaN(departmentId)) { responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); } return departmentVO; } But you still have a "return departmentVO" to Return a object from your function. This is null object if you call this method. it is impossible to RETURN departmentVO in Asyc function. That is my point. anyway your LazyLoaderResponder looks clean Could you post your LazyLoaderResponder ? Thanks 这个问题问得非常好,我也确实遇到这个问题,但经过测试后,没有发现问题 我在server端的load department方法里加了一个Thread.sleep(10000); 测下来没有问题 另外,我有一个疑惑还没有来得及研究,那就是,如果我的datagrid只有一条记录,为什么我的label function会被调用4次,两次departmentVO为null,两次是有值的 LazyLoaderResponder的代码 package com.xxxxx.common.responder { import mx.controls.Alert; import mx.rpc.IResponder; public class LazyLoaderResponder implements IResponder { private var _target : Object; private var _fieldName : String; public function LazyLoaderResponder(target:Object, fieldName:String) { _target = target; _fieldName = fieldName; } public function result( data : Object ) : void { _target[_fieldName] = data; } public function fault( info : Object ) : void { Alert.show("Lazy load "+_target+"."+_fieldName+" failed!"); } } } |
|
返回顶楼 | |
发表时间:2008-01-29
JavaJason 写道 houwei 写道 public function getDepartmentVO():DepartmentVO
{ var responder:LazyLoaderResponder; if(departmentVO == null && !isNaN(departmentId)) { responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); } return departmentVO; } But you still have a "return departmentVO" to Return a object from your function. This is null object if you call this method. it is impossible to RETURN departmentVO in Asyc function. That is my point. anyway your LazyLoaderResponder looks clean Could you post your LazyLoaderResponder ? Thanks 这个问题问得非常好,我也确实遇到这个问题,但经过测试后,没有发现问题 我在server端的load department方法里加了一个Thread.sleep(10000); 测下来没有问题 另外,我有一个疑惑还没有来得及研究,那就是,如果我的datagrid只有一条记录,为什么我的label function会被调用4次,两次departmentVO为null,两次是有值的 LazyLoaderResponder的代码 package com.xxxxx.common.responder { import mx.controls.Alert; import mx.rpc.IResponder; public class LazyLoaderResponder implements IResponder { private var _target : Object; private var _fieldName : String; public function LazyLoaderResponder(target:Object, fieldName:String) { _target = target; _fieldName = fieldName; } public function result( data : Object ) : void { _target[_fieldName] = data; } public function fault( info : Object ) : void { Alert.show("Lazy load "+_target+"."+_fieldName+" failed!"); } } } I guess it is ur datgrid has more than 1 column, u can try to cut ur column to see if u get less call for ur lable function |
|
返回顶楼 | |
发表时间:2008-01-29
houwei 写道 JavaJason 写道 houwei 写道 public function getDepartmentVO():DepartmentVO
{ var responder:LazyLoaderResponder; if(departmentVO == null && !isNaN(departmentId)) { responder = new LazyLoaderResponder(this, "departmentVO"); ServiceFactory.getService(ServiceFactory.DEPARTMENT_BIZ).callServiceWithResponder(ServiceFactory.DEPARTMENT_BIZ_LOAD, [departmentId], responder); } return departmentVO; } But you still have a "return departmentVO" to Return a object from your function. This is null object if you call this method. it is impossible to RETURN departmentVO in Asyc function. That is my point. anyway your LazyLoaderResponder looks clean Could you post your LazyLoaderResponder ? Thanks 这个问题问得非常好,我也确实遇到这个问题,但经过测试后,没有发现问题 我在server端的load department方法里加了一个Thread.sleep(10000); 测下来没有问题 另外,我有一个疑惑还没有来得及研究,那就是,如果我的datagrid只有一条记录,为什么我的label function会被调用4次,两次departmentVO为null,两次是有值的 LazyLoaderResponder的代码 package com.xxxxx.common.responder { import mx.controls.Alert; import mx.rpc.IResponder; public class LazyLoaderResponder implements IResponder { private var _target : Object; private var _fieldName : String; public function LazyLoaderResponder(target:Object, fieldName:String) { _target = target; _fieldName = fieldName; } public function result( data : Object ) : void { _target[_fieldName] = data; } public function fault( info : Object ) : void { Alert.show("Lazy load "+_target+"."+_fieldName+" failed!"); } } } I guess it is ur datgrid has more than 1 column, u can try to cut ur column to see if u get less call for ur lable function 仍然有4次调用的,并且不幸的是,有两次都会去server端拿值,这个比较麻烦 我后面再研究研究这个问题,看看到底是哪里派发了四次这个事件 |
|
返回顶楼 | |
发表时间:2008-01-29
Can u upload all your source code . Thx
|
|
返回顶楼 | |
发表时间:2008-01-29
houwei 写道 Can u upload all your source code . Thx
太大了,我发邮件给你,告诉我,你的邮箱吧 --管理员的分割线-- JavaEye禁止在论坛留邮箱,请用站内短信 |
|
返回顶楼 | |
发表时间:2008-01-29
ooxx@ooxx.ooxx
--管理员的分割线-- JavaEye禁止在论坛留邮箱,请用站内短信 |
|
返回顶楼 | |