Request for Comments
This project solves problems of
boilerplate
// for example you have collection/users.jsreturn BackboneCollection; // and collection/things.jsreturn BackboneCollection; // and model/thing.jsreturn BackboneModel; // file-that-uses-things.jsvar Things = things = ; // file-that-uses-thing.jsvar Things = things = id: maybeId ;
You get the idea, this is called boilerplate. This project simplifies your model layer at the cost of flexibility in terms of custom code.
// api.jsreturn prefix: '/api' resources: user: 'any' thing: 'any' // model/collection defenition // file-that-uses-things.jsvar api = things = api; // returns an instance // file-that-uses-thing.jsvar api = things = api;
synchronisation and singleton management
// usually you would have a manager kind of thing, that would keep track of your instances// manager.jsvar Things = things = ;return things: things;// file-that-uses-things.jsvar manager = things = managerthings;// file-that-uses-thing.jsvar manager = things = managerthings thing = things;
Regarding that last part, things
may not be up to date and have no such item. If you create a separate model, but then there is again the sync question. It's a slippery slope. This project manages sigletons for you.
// file-that-uses-things.jsvar api = things = api; // file-that-uses-thing.jsvar api = thing = api;
If things
has an item with such id, then it would be the same reference. If things
would then be fetched, and got the new thing, it would be the same reference.
schema mappings
If you are uncofortable with your schema, because it involves
- odd to javascript naming conventions
like_this
or it.is.very.unnecessary.deeply.nested
orit['com.this.like.gems.has']
or- it has
abbrs
instead of readableabbreviations
GET /api/things/1
in Backbone you would
// and model/thing.jsreturn BackboneModel; // and collection/things.jsvar Thing = ;return BackboneCollection; // now you have to mention your custom model, because it's not generic
Or you could just
// api.jsreturn prefix: '/api' resources: user: _shortcutMappings: price: 'details' 'prc' name: 'details' 'name' integratedServices: 'integrated_services' thing: 'any'
This will intercept all get
s/set
s and correct the keys.
Features
- models with defined ids are singletons
- once id changes (ex. current user is fetched) the model will be tied to an existing model with same id
- collections also point to singleton models, thus sync is free
- sort of type validation
- adaptation to other (than backbone) framework's models is possible
How to Use
// m-for-my-api.js;
// current-group-mvwhaterver.jsvar m = ;var usersInMyGroup = m;
// selected-user-mvwhaterver.jsvar m = ;var user = m;
// current-user-mvwhaterver.jsvar m = ;var me = m;meme// {// id: 1,// name: 'Herman Starikov',// attributes: [{// name: 'com.m.lang',// value: 'ru'// }],// association: {// flags: {// 'com.m.administrator': 'true'// }// }// }