Displaying list from databse in gridview

roelof's Avatar

roelof

15 Jun, 2016 06:05 PM

Hi,
I am not sure if this question is related to this: http://knowledge.robotlegs.org/discussions/robotlegs-2/13003-inject-models-into-specific-purpose-view
, but here is my question anyway.
Say for example I have a list of students store in a sqlite db and I want to display the whole list in a gridview, what is the recommended way of doing it in RL?
I created a service that returns an ArrayCollection, and on the view side, I created a Bindable ArrayCollection that is used as a DataProvider for a DataGrid. How do I link the ArrayCollection in the service to the ArrayCollection in the View?

Do I dispatch an event from the service, with the ArrayCollection as the payload?

  1. 1 Posted by roelof on 15 Jun, 2016 06:15 PM

    roelof's Avatar

    This might provide some kind of answer, but I am not sure how to implement this?

    http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/can-i-access-models-and-services-in-my-mediators

  2. Support Staff 2 Posted by Ondina D.F. on 16 Jun, 2016 11:09 AM

    Ondina D.F.'s Avatar

    Hi Roelof,

    Do I dispatch an event from the service, with the ArrayCollection as the payload?

    There are many ways of accessing services and models, as you've noticed.

    I'm a proponent of using events.
    If you don't need a Model for your data, then you can let the mediator(s) listen to an event dispatched by the service with the result as a payload.

    In my example below, the mediator makes a request for data in its initialize() method, just to illustrate the flow of events. SomeCommand reacts to the event dispatched by the mediator and accesses the service. When the service gets the result from the db it dispatches an event with the result as the payload. The mediator added a listener for that event so that it can receive the db data and pass it on to the view. You can run the example to see how it works.

    Let me know if that helps or if you need more assistance.

    Ondina


    Example:

    Mappings:

    mediatorMap.map(SomeView).toMediator(SomeMediator);
    
    injector.map(ISomeService).toSingleton(SomeService);
    
    commandMap.map(SomeEvent.STUDENT_DATA_REQUESTED).toCommand(SomeCommand);
    

    View:

    public function setGridDataProvider(loadedDataProvider:ArrayList):void 
    {
        someGrid.dataProvider = loadedDataProvider;
    }
    

    Mediator:

    public class SomeMediator extends Mediator
    {
        [Inject]
        public var view:SomeView;
    
        override public function initialize():void
        {
        //first add a listener for SomeEvent.STUDENT_DATA_RECEIVED
            addContextListener(SomeEvent.STUDENT_DATA_RECEIVED, onStudentDataReceived, SomeEvent);
        //then make the request
            dispatch(new SomeEvent(SomeEvent.STUDENT_DATA_REQUESTED));
        }
    
        private function onStudentDataReceived(event:SomeEvent):void
        {
            view.setGridDataProvider(event.somePayload);
        }
    }
    

    Event::

    public class SomeEvent extends Event
        {
            public static const STUDENT_DATA_REQUESTED:String = "studentDataRequested";
            public static const STUDENT_DATA_RECEIVED:String = "studentDataReceived";
    
            private var _somePayload:ArrayList;
    
            public function SomeEvent(type:String, somePayload:ArrayList=null, bubbles:Boolean=false, cancelable:Boolean=false)
            {
                super(type, bubbles, cancelable);
                _somePayload = somePayload;
            }
    
            public function get somePayload():ArrayList
            {
                return _somePayload;
            }
    
            public override function clone():Event
            {
                return new SomeEvent(type, somePayload, bubbles, cancelable);
            }
        }
    

    Command:

    public class SomeCommand
    {
        [Inject]
        public var someService:ISomeService;
            
        public function execute():void
        {
            someService.loadRemoteData();
        }
    }
    

    Service:

    public class SomeService implements ISomeService
    {
        [Inject]
        public var sharedEventDispatcher:IEventDispatcher
            
        public function SomeService()
        {
        }
        public function loadRemoteData():void
        {
            //select from db  
            //here mocking the db data and the result's handler
            var fakeResult:ArrayList = new ArrayList([
            {Student:'Ondina', Question:'Why is the sky blue?'},
            {Student:'Roelof', Question:'Why is the grass green?'}]);
            
            onRemoteDataLoaded(fakeResult);
        }
        private function onRemoteDataLoaded(result:ArrayList):void
        {
            // read the result and send it as the payload of SomeEvent
            // here sending the fake result as array list:
            sharedEventDispatcher.dispatchEvent(
            new SomeEvent(SomeEvent.STUDENT_DATA_RECEIVED, result));
        }
    }
    
    public interface ISomeService
    {
        function loadRemoteData():void
    }
    
  3. 3 Posted by roelof on 17 Jun, 2016 04:33 AM

    roelof's Avatar

    Hi Ondina,

    Your example makes perfect sense, and it fits quite well into my current project implementation. I will test it and let you know.

    One last question on this topic, and this might actually be on AS3 level: When you pass the Arraylist as a payload, does RobotLegs / AS3 actually only pass a pointer to the current Arraylist in memory? If my query returns a result of, say 10,000 records with a lot of information per row, will above method still be recommended?

  4. 4 Posted by 2morrowman on 17 Jun, 2016 09:51 AM

    2morrowman's Avatar

    Hi Ondina,

    why in all examples mediators never updates a view on initialization?

    My regular mediator looks like this:

    public class SomeViewMediator extends Mediator {
      [Inject]
      public var view:SomeView;
      [Inject]
      public var someModel:SomeModel;
      override public function initialize():void {
        updateView();
        addContextListener(SomeEvent.DATA_UPDATE, updateView);
      }
      protected function updateView( event:Event = null ):void {
        view.data = someModel.data;
      }
    }
    
  5. Support Staff 5 Posted by Ondina D.F. on 17 Jun, 2016 02:22 PM

    Ondina D.F.'s Avatar

    Hi Roelof and 2morrowman,

    @Roelof

    One last question on this topic, and this might actually be on AS3 level: When you pass the Arraylist as a payload, does RobotLegs / AS3 actually only pass a pointer to the current Arraylist in memory?

    That has nothing to do with robotlegs. :)
    In as3 all arguments are passed by reference, including primitive data types (Boolean, String, Number, etc) .
    The only difference between primitive and complex data types is that primitives behave like they were passed by value, because as3 stores them internally as immutable objects.
    For more details see Adobe's documentation.

    If my query returns a result of, say 10,000 records with a lot of information per row, will above method still be recommended?

    Nowadays, 10,000 records shouldn't take much time loading.
    But, in my experience, the best way to deal with large data sets is to implement a 'paginator'.
    I always display a limited number of results in a datagrid. I use buttons to navigate the data set, and to load subsets of data. Search the internet for pagination, paginated datagrid, data paging.. if you're interested in this approach.

    I'm curious, how have you dealt with >10,000 records before using any mvc framework?

    @2morrowman

    That's really a matter of personal preferences and project requirements.

    There are people who would never inject a Model into a Mediator.
    Then there are others who would inject Models and Services into Mediators or even into Views.
    I think, either approach can be valid, but it depends on the overall structure and requirements of a certain project and the experience of the developers how it is implemented.

    I tend to use events for the communication between classes, but I also have use cases in my projects where injecting a model into a mediator makes sense and it also doesn't create too much coupling.

    So, your snippet of code looks fine, and if it works for you, go for it. :)

    Ondina

  6. 6 Posted by roelof on 17 Jun, 2016 03:43 PM

    roelof's Avatar

    Hi Ondina,
    I thought that it is not Robotlegs relevant, but thank you for answering anyway :-)

    I understand the concept of pagination, but at this stage my question was more about the underlying memory footprint and as3 behaviour.

    I have not previously worked with >10,000 records directly, was more involved on the QA side of such projects. I am busy with feasibility study on a possible project that will have that amount of records, but it is too early to be exact.

  7. Support Staff 7 Posted by Ondina D.F. on 21 Jun, 2016 01:21 PM

    Ondina D.F.'s Avatar

    Sorry for the delayed response.

    I put together a few links to articles about arrays, memory management, workers.. that might be of interest to you. I'm going to mark this thread as resolved for now. You are free to re-open it if you think that there are some robotlegs-related aspects of your question that have not been answered yet. And, of course you can create new threads for other issues you may encounter as your project progresses :)

    Ondina


    • Array vs Vector vs ByteArray:

    http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/...

    http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118676...

    http://homecoded.com/blog-entry-135.html

    • Memory management + garbage collection

    http://gskinner.com/talks/resource-management/
    http://blog.gskinner.com/archives/2006/06/as3_resource_ma.html
    http://www.adobe.com/devnet/flashplayer/articles/garbage_collection...
    https://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVs...

    http://help.adobe.com/en_US/as3/dev/WS0191790375a6482943a72da3138ec...

    http://www.bytearray.org/?p=4423

    http://jacksondunstan.com/articles/2442

  8. Ondina D.F. closed this discussion on 21 Jun, 2016 01:21 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac