How to use Models and VO's

Nangra's Avatar

Nangra

12 Jan, 2012 08:45 PM

Hi,

for example i have a book,
every book have pages,
every page have text + images.

Should i have BookModel instance + PageModel instance for every PageView instance?
Or just one BookModel and one PageModel instance? (But how then PageModel will know on which page I'm asking, every method will have pageIndex?)

  1. 1 Posted by Pierre Laveklin... on 12 Jan, 2012 09:33 PM

    Pierre Laveklint's Avatar

    I think I would go with:

    BookModel
    hold reference to a vector of pages.

    PageVO
    holds the data text and ref to images that get passed to each page-View upon request

  2. 2 Posted by Nangra on 12 Jan, 2012 09:34 PM

    Nangra's Avatar
    PageModel{
        public function getImages():Array
        {
            // How this model will know of witch page images i'm asking?
        }
    }
    
  3. 3 Posted by Nangra on 12 Jan, 2012 09:35 PM

    Nangra's Avatar

    Pierre Laveklint

    But VO shouldn't have any logic inside. So how can I for example add a new Image and dispatch event about this.

  4. 4 Posted by Pierre Laveklin... on 12 Jan, 2012 09:51 PM

    Pierre Laveklint's Avatar

    Correct, a VO is just a value object and should not handle any logic.

    In BookModel you can set up a pageIndex which matches current page-id.

    BookModel

    _pageIndex:int = 0; // make getter + setter 
    public function getCurrentPage():PageVO
    {
        return pages[pageIndex];
    }
    

    PageVO

    public var textContent:String;
    public var imagePath:String
    

    Make sense?

  5. 5 Posted by Nangra on 12 Jan, 2012 09:57 PM

    Nangra's Avatar

    But how to get the 5th image from the 4th page

    bookModel.getCurrentPage(). // And what now? I got VO which don't have method to get image by index

    So should I create PageModel? But one for every VO? That don't make sense:/

  6. 6 Posted by Pierre Laveklin... on 12 Jan, 2012 10:12 PM

    Pierre Laveklint's Avatar

    You can set the image variable to a vector/array of strings (paths to images).
    When you received the PageVO of your choice you can grab the image you want.

    var pageVO = model.getCurrentPage()
    trace(pageVO.images[4])
    

    if you need to add/remove pictures to a page during runtime the I would consider making a PageModel as well.

  7. 7 Posted by Nangra on 12 Jan, 2012 10:17 PM

    Nangra's Avatar

    I need more complicated methods than just getImage, this is just example.

    But sa You said, making PageModel... How PageModel would know about which PageVO I am asking him.

    BookModel is easy becouse i have just one instance of Book, but pages are unlimited and just one PageModel :|

    Can You give me a short example with BookModel and PageModel where BookModel return a page instance, and a PageModel return any image from page which was get from BookModel?

  8. 8 Posted by Pierre Laveklin... on 12 Jan, 2012 10:27 PM

    Pierre Laveklint's Avatar

    Sorry, typing on my iPhone and I just posted a a quite long answer but seems it didn't make it in here for some reason. I have to run now, will see if I get the time tomorrow to set up an example.

  9. Ondina D.F. closed this discussion on 29 Feb, 2012 11:14 AM.

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