I looked into using Backbone for my current project, and took a peek at several other MVC libs as well. In the end I decided they all did way more than I was looking for. I just wanted a simple way to separate my models from the DOM. This project seems better than anything else I've seen, but still, 1000 loc seems like a whole lot.
Here's how I'm currently doing it:
window.models = {}
function json_to_model(json, parent_model) {
// Create initial attrs.
var id = '' + Math.round(Math.random() * 100000000000)
var children = json.children
delete json.children
json['id'] = id; json.is_expanded = true
json.child_ids = []; json.parent_id = null;
if(parent_model) {
json.parent_id = parent_model.get('id')
var child_ids = parent_model.get('child_ids')
child_ids.push(json.id)
parent_model.set('child_ids', child_ids)
}
// Store models. Recurse children.
var new_model = {attrs:json}
add_get_set(new_model)
window.models[id] = new_model
render_model(new_model)
if(children) {
for(var i = 0; i < children.length; i++)
json_to_model(children[i], new_model)
}
return new_model
}
// Get and set. Render on set.
function add_get_set(model) {
model.get = function(key){ return this.attrs[key] }
model.set = function(key, val) {
this.attrs[key] = val
render_model(this)
localStorage.setItem('models', JSON.stringify(window.models))
}
}
Now, that doesn't do much. But for prototyping a gui, it seems pretty good. Less is more. I wonder how little code I can add to get it to work with a real backend.