"Ross Sclafani" <ross.sclaf...@gmail.com> wrote:
It is very easy to locate any code in one of my projects by ascertaining the 
domain of the code in question and looking in the appropriate branch.
Does it store data? It's in the model. Does it interpret and display data? Try 
your views. Does it manipulate data? Look in the controller.

This thread has been useful, thanks all. I've a ton of questions regarding 
judgment calls and below I post a class illustrating a few I've struggled with. 
The comments are intended to be my questions/admissions of bafflement. I'm 
unsure where in a MVC this class should go as its main purpose is to work with 
the File class which itself has methods which retrieve 
(File.applicationDirectory), interpret (File.exists) and display 
(File.browseForOpen) data.

The class also is a dreaded example of allowing the view to listen directly to 
the model for events, perhaps only because I've misguidedly decided to make it 
part of the model as it has to do with copying and deleting a SQLite file used 
in the app.

package mvc.model {
 /* saveFileAs() saves a copy of a SQLite DB for the purposes of
 transferring data to an instance of this app on another
 computer.

closeDBAndReplace() = replaces the db file if the user
 is importing data.
 */
 import flash.events.EventDispatcher;
 import flash.events.Event;
 import flash.filesystem.File;
 // class Data works with a SQLite DB
 import mvc.model.Data;
 // Where in a MVC should custom event classes
 // be located? I wish to pass my own objects
 // along with events, usually "Transfer Objects"
 // or a string to be displayed
 import mvc.controller.CustomDataEvent;

 public class ManipulateDBFile extends EventDispatcher {

  private var _data:Data;
  private var _sourceFile:File;
  private var _copyToDirectory:File;

  public function ManipulateDBFile(data:Data) {
   _data = data;
  }

  public function saveFileAs() : void {
   var docsDir:File = File.desktopDirectory;
   // This creates a UI element. I would look for this code in the view!
   docsDir.browseForDirectory('Save File in  ...');
   // This is asking a UI elemt to inform the Model directly. Big bad no?
   docsDir.addEventListener(Event.SELECT, copyFile);
  }

  private function copyFile(e:Event):void  {
   _sourceFile = File.applicationStorageDirectory.resolvePath("msgDB.db");
   _copyToDirectory = e.target.resolvePath("msgDB.db");
   if (_copyToDirectory.exists) {
    // Passing this event through the Controller seems to create complexity,
    // or at least unnecessary lines of code. Is there an advantage gained by
    // communicating to the view through the controller here?
    var evt:CustomDataEvent = new 
CustomDataEvent(CustomDataEvent.FILE_ALREADY_EXISTS);
    dispatchEvent(evt);
   }
   else {
    replaceFile();
   }
  }

  public function replaceFile() : void {
   var evt:CustomDataEvent = new CustomDataEvent(CustomDataEvent.COPY_COMPLETE);
   try {
    _sourceFile.copyTo(_copyToDirectory, true);
    dispatchEvent(evt);
   }

   catch (error:Error) {
    evt.param = error.message;
    dispatchEvent(evt);
   }
   _sourceFile = null;
   _copyToDirectory = null;
  }

  public function closeDBAndReplace() : void {
   // The file cannot be deleted if there is a SQLConnection to it.
   // The class that is aware of a possible connection also does the
   // deletion. But deleting the file seems to conceptually
   // fit into this class better
   _data.addEventListener(CustomDataEvent.DRILL_RESET, findFile, false, 0, 
true);
   _data.deleteDBFile();

  }

  private function findFile(e:CustomDataEvent) : void {
   _data.removeEventListener(CustomDataEvent.DRILL_RESET, findFile, false);
   var docsDir:File = File.desktopDirectory;
   docsDir.browseForOpen('Select msgDB.db file ...');
   docsDir.addEventListener(Event.SELECT, replaceDBFile);
  }

  private function replaceDBFile(e:Event):void  {
   var sourceFile:File = e.target as File;
   var destination:File = 
File.applicationStorageDirectory.resolvePath("msgDB.db");
   try {
    sourceFile.copyTo(destination, true);
    dispatchEvent(new CustomDataEvent(CustomDataEvent.RESTART_REQUIRED));
   }
   catch (error:Error) {
    trace("Error:", error.message);
   }
  }
 }
}

On 2/17/12 6:07 PM, "Ross Sclafani" <ross.sclaf...@gmail.com> wrote:
It is very easy to locate any code in one of my projects by ascertaining the 
domain of the code in question and looking in the appropriate branch.
Does it store data? It's in the model.
Does it interpret and display data? Try your views.
Does it manipulate data? Look in the controller.



_ _ _
Erik Mattheis | Weber Shandwick
P: (952) 346.6610
M: (612) 377.2272
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to