petermichaux / maria

The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.
BSD 2-Clause "Simplified" License
764 stars 51 forks source link

Models - a more complex model #82

Closed martinwi closed 10 years ago

martinwi commented 10 years ago

I am desperately trying to get a more complex model working with Maria and wondered if you would be kind enough to point me in the right direction. It’s my first attempt at writing an MVC based application and I’m struggling to comprehend the best approach to start off with.

In an attempt to create an itinerary for a road trip, I have a model in mind, but can think of various approaches.

Should I be creating different models for each subcomponent/portion of my object model or dealing with it as one? Using your TodoModel.js structure, I’m attempting to shoehorn in my travel itinerary “trip” object:

maria.Model.subclass(trip, 'TripModel', {
   properties: {
       _content: {},
//          {   "type": "motorcycle"
//              ,"start": {"gps": "12345", "eta": "09:30", "name": “Den Bosch"}
//              ,"end": {"gps": "12345", "eta": "10:45", "name": “Rotterdam"}
//              ,"distance": "75"
//              ,"duration": "1:30"
//              ,"note": []
//              ,"route": [
//                      {"type": "location"
//                          ,"gps": "123456"
//                          ,"eta": "10:40"
//                          ,"name": "'s-Hertogenbosch Station"
//                          ,"url": ""
//                          ,"note": []
//                      }
//                      ,{"type": "location"
//                          ,"gps": "123456"
//                          ,"eta": "10:45"
//                          ,"name": "P&O - Luxembourg Weg No. 2, 3198 LG Europoort, Netherlands"
//                          ,"url": "http://www.poferries.com/tourist/content/pages/template/routes_hull_-_rotterdam_rotterdam_rotterdam.htm"
//                          ,"note": ["Directions: The terminal in Europoort lies approximately 24 miles from the centre of Rotterdam. On approaching Rotterdam, follow the signs for 'Europoort'. In the Europoort area, just follow the signs 'England' and 'P&O Ferries'. The dock number (Havennummer) of P&O Ferries is 5805."]
//                      }
//                      ,{"type": "map"
//                          ,"img": "img/route11.png"
//                          ,"url": "https://goo.gl/maps/uJFKQ"
//                      }
//                  ]
//              }
//          },
       _isDone: false,
       getContent: function() {
           return this._content;
       },
       setContent: function(content) {
           //content = checkit.trim('' + content);
           if (this._content !== content) {
               this._content = content;
               this.dispatchEvent({type: 'change'});
           }
       },
       isDone: function() {
           return this._isDone;
       },
       setDone: function(isDone) {
           isDone = !!isDone;
           if (this._isDone !== isDone) {
               this._isDone = isDone;
               this.dispatchEvent({type: 'change'});
           }
       },
       toggleDone: function() {
           this.setDone(!this.isDone());
       }
   }
});

I’d really appreciate your thoughts on the best approach for this, so I make best use of Maria, as it was intended.

Thanks Martin...

petermichaux commented 10 years ago

Hi Martin,

I don't know much about your application. Specifically, I don't know how the views want to use the data in the model and how data is sent to and from the server. Even so, it looks to me like your content in this model is far too complex. My guess is that it would be better to break it up into individual properties.

For example, my guess is you should have a location model

    maria.Model.subclass(trip, 'LocationModel', {
        properties: {
            _gps: '',
            _eta: '',
            _name: '',
    // ...

Then in your trip.TripModel, the start property would be an instance of a trip.LocationModel.

martinwi commented 10 years ago

Thanks Peter.

That makes sense.

My initial thoughts were to start with a generic (and simple) "note" model, which largely emulates the TodoModel, but I'm not sure how I'd roll those up into an Array of notes to be used by other objects in the model.

Can a maria.SetModel class contain other maria.SetModels, or am I trying to make this more complex than it needs to be?

Thanks Martin...

petermichaux commented 10 years ago

A maria.SetModel instance can contain other maria.SetModel instances. Events bubble up through the whole tree.

If you have more questions, feel free to ask.