Agility.js - Javascript MVC for the "write less, do more" programmer
agilityjs.com
agilityjs.com
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.If so, and your project is sizable, then an MVC, like backbone.js, could be beneficial.
eg
parent_model = window.models[foo_model.get('parent_id')]
parent_model.set('text', 'foo text')
When you respond to a button click or whatever, then yeah, you use a jQuery callback. The model id is stored in the DOM element (the view) which you access with $(this). Whenever the model changes (via set), the render function is automatically called. The render function is responsible for passing attributes to a template which renders html. Then the render function sticks the html wherever it's supposed to go. I think that's basically how all MVC frameworks work under the hood.Each to their own! :-)
PS: You can already skip in-object CSS.
Also, because Agility allows me to refer to the root element of a view as the selector "&", I don't have to worry about maintaining that additional id/class. This becomes especially useful when you have many MVC objects (as most projects do), each with their own root element. Suddenly you have eliminated dozens of unnecessary ids/classes.
What flavors of server-side frameworks would go well with it?
Check out the The Wall demo code for a Node.js example:
Although I like data-bind stuff - really neat!
I don't necessarily agree with that Backbone comment. Separation of M-V-C (which Agility does too) is merely a fine-grained level of code organization, and it doesn't help with a large code base with many different abstract objects/components.
For example, as you can see from the following code (from our demo app The Wall), the most significant aspect of code organization comes from a separation of concepts - such as "error message", "posts", "profile", "stream", etc - as separate objects:
https://github.com/arturadib/thewall/blob/master/public/app....
But again, Agility supports the more explicit syntax "model:{}", "view:{}", "controller:{}", if that's what you think helps you with your large code base.
On the other hand, I don't like getting an unsolicited message about the project just because I'm watching Backbone.js on GitHub. I was mostly indifferent to Agility.js before, now I have some dislike of it.
Thanks!
There's no need to go around multiple HTML/CSS/JS files looking for the ids/classes I need to maintain. It's all right there.
(I agree that the need for a backslash is annoying, but the tradeoff is favorable in my opinion).
That being said, you don't have to use in-object CSS, and very soon we'll be relaxing the HTML requirement as well.
For bigger projects I would do the development of different parts of the app in different JS files and have a server side script combine them when requested from a web browser.
Feel free to contribute your own adapter - it's really easy to create one! Get in touch if you need help.
One suggestion : maybe not perfect but, for html templates you can do what jQuery does: templates/html in <script> tag, user just gives the tag's id. this way, one can store all templates in one file.
If you could blur that browser-server line, I bet you could do some impressive things. Bonus points if you manage to do impressive things without opening up lots of security holes. :-)