Which architectural pattern allows dynamic instantiation of multiple views per model? -


let's want build calendar app (in html + js) user can see 1 week @ time.

the user should able define events, e.g. mondays 1:00 - 3:00:

         +---------+---------+     +---------+          | monday  | tuesday | ... |  sunday |          +---------+---------+     +---------+ 0:00     |         |         |     |         |          +---------+---------+     +---------+ 1:00     ///////////         |     |         |          //myevent//---------+     +---------+ 2:00     ///////////         |     |         |          +---------+---------+     +---------+ ...                                             +---------+                  ... 23:00    |         |   ...          +---------+ 

i thought creating 1 model, eventmodel , 1 view, eventview, , wiring them usual mvc approach. eventview rendered on top of calendar grid separate layer, able autonomously maintain size , position.
eventmodel has nothing 3 attributes, starttime, duration , title.

things become more difficult though, if user defines event spans on midnight, mondays 23:00 - tuesdays 2:00:

         +---------+---------+     +---------+          | monday  | tuesday | ... |  sunday |          +---------+---------+     +---------+ 0:00     |         ///////////     |         |          +---------//myevent//     +---------+ 1:00     |         ///////////     |         |          +---------+---------+     +---------+ 2:00     |         |         |     |         |          +---------+---------+     +---------+ ...                                             +---------+                  ... 23:00    //myevent//   ...          +---------+ 

now 2 views bound same model needed.

naturally, changes in model should reflected in view(s). note number of views required single model may change (1 7) starttime or duration attributes updated.

in other words: changes in model need trigger instantiation/destruction of views model.

i see 2 possible approaches achieving this:

  • use controller logic dynamically generates/deletes required number of views , models, , keeps models in sync each other

... seems bad idea since require lot of fumbling additional class of models.

or

  • stick 1 model , build view proxy acts 1 view, internally controls required number of "subviews".

the second approach more appealing me. wrote pseudo-code illustrate it:

viewproxy.onmodelchange(model) {     if (model.haschanged("starttime") or model.haschanged("duration"))         this.destroysubviews()         substarttimes[] = getsubstarttimes(model.starttime, model.duration)         subdurations[] = getsubdurations(model.starttime, model.duration)         subviews[] = this.buildsubviews(substarttimes, subdurations)         subviews.bind(model, "title")     endif } 

the downside subviews unnecessarily destroyed , re-built on model updates (except title only), unless include additional logic - not trivial. also, proxy weird thing between controller , view, somehow makes me feel uncomfortable.

so question is: there standard approach problem? if not, issues may arise above solution? other solutions?

your eventmodel maps multiple view elements because model contains multiple sub elements (eventdaymodel).

sure, input definition of eventmodel might title, startdatetime, duration, model conceptually consists of 1 or more eventdaymodel elements (each containing starttime , duration).

so instead of maintaining 1 one mapping between eventmodel , eventviewproxy (which controls view elements corresponding each day), why not subdivide eventmodel list of eventdaymodel, each have 1 one mapping eventdayview elements.

when change eventmodel, it's eventdaymodel list recalculated , corresponding eventdayview elements updated.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -