jquery - Issue structuring JavaScript (separation of view and model logic) -
i'm working on javascript-heavy piece (using jquery) website (at moment, i'm working on getting prototype going).
to summarize i'm doing, have series of 'icons'. each icon, @ moment, image. have series of 'buckets'. icons start in single bucket. user able create new buckets , drag icons around bucket bucket. icons can turned on , off clicking on them (although still remain in bucket).
i have working fine @ moment, @ moment it's bunch of img elements getting moved around div div. when i'm ready start implementing server-side logic, i'm going need way communicate what's going on server.
what need 'model' object reflecting what's going on view. it'd if when user ready send data server, i'd have object serialize representing need. example, if user has dragged email.png icon bucket options bucket, reflected in model object (i.e. { 'options': ['email'] }).
the issue is, logic occurring based on events. when user drags icon div, event fired on img dom element, i'm not sure how access it's model update it. thing can think of parsing img src , use find out name of model option that's hackish , inelegant solution.
any ideas?
you maintain raw object model or graph of objects attaching root object in dom, or alternatively using jquery data system keep in.
even reference ui dom elements centralized model reduce complexity of 2 distinct models.
it's not uncommon jquery plugins centralize state in manner seek, inside jquery data system.
raw javascript
document['myobjectgraph'] = { first: 1, second: 'two, images: [ document.getelementbyid('img1'), document.getelementbyid('img2') ], items: [ 'a', 'b', = { /* building out model...*/ }; ] };
or jquery data system (same idea)
jquery.data(document.body, 'myobjectgraph', { first: 1, second: 'two, images: [ $('#img1'), $('#img2') ], items: [ 'a', 'b', = { /* building out model...*/ }; ] });
effectively creates manageable model in 1 place (that choose design of) - represents state of icons, buckets , else; can serialized using json , sent server through ajax call; , ui elements can rebuilt/refreshed it.
this idea comes cost of indirection , effort of keeping synchronized raw model ui model.
Comments
Post a Comment