5 ms·
hi, i've been experimenting a bunch with different approaches to doing MVC in Javascript. I did one project where I completely rolled my own rails-like framewor
by marstall 16y ago
hi, i've been experimenting a bunch with different approaches to doing MVC in Javascript. I did one project where I completely rolled my own rails-like framework, with a base controller, base model & view functionality based on EJS (my favorite js templating library).
Then I discovered sammy.js, which provides a nice approach to routing and linking. my current project uses that, and i roll my own data access code & use EJS for templating. I also have written all my own glue.
The space is calling out for a "Javascript on Rails" that takes a variety of different libraries and combines them in a canonical way so that each new project doesn't have to go through a shopping expedition, deciding which libraries are currently the best.
What's needed, basically:
1. Routing, navigation + back-button. Single-page JS apps operate very differently from normal apps because many links are hash links, eg. #settings. Sammy.js handles this nicely.
2. models. Here I have a different definition of what a model is than Backbone. I see a model as a class that wraps a particular service. So you might have a TwitterModel, YouTubeModel, MyServiceModel, etc. They would all benefit from inheriting from a base class that does the necessary networking (JSONP, JSON, etc.). It would be nice if all data came back from the service in the form of a Backbone.js Collection.
3. Views. There is a lot of code that ends up being needed around rendering templates. Think of the variety of different render() functions that exist in rails. That translates to Javascript, in my experience. Rendering templates into a div, doing pre- and post- processing, even swapping rendered blocks into invisible divs so they can be swapped back in quickly when the user hits the back button.
I could go on about the neat things that could/should be done, including all the declarative programming and "convention over configuration" stuff that rails does (example: render_foo() loads template foo.ejs and renders it into <div id='foo'>)
Anyway I feel myself a bit lost in a sea of googling whenever I try to find the definitive projects in this space - so I hope some will comment with their fave MVC-esque projects for Javascript...
- HenrikJoreteg 16y agoSammy.js does look quite cool. I've poked around the documentation, but haven't built anything with it yet. I see what you're saying about "Javascript on Rails" but to me that's quite different than something like backbone. What you're talking about is more about "ajaxifying" a traditional browse-from-page-to-page experience. I definitely think there's value in that for improving the feel and responsiveness of a site. However, I'm more interested in truly emulating the desktop app experience where, once you log in there is no concept of browsing from page to page or "going back".
- brianjesse 16y agoHave you seen Qooxdoo? I've made some very desktop-like apps with it http://demo.qooxdoo.org/current/showcase/#form http://demo.qooxdoo.org/current/showcase/#form
- jashkenas 16y agoThere's a ticket that proposes adding a "hashchange" router to Backbone, if you're interested in contributing your thoughts: http://github.com/documentcloud/backbone/issues#issue/35 http://github.com/documentcloud/backbone/issues#issue/35 The big question is if it's useful enough -- Models/Collections/Views are used constantly, whereas hashchange routing is a comparatively tiny part of an app. For the record, the previous Backbone+Sammy discussion is here: http://news.ycombinator.com/item?id=1788456 http://news.ycombinator.com/item?id=1788456 And there's another on the Sammy list: http://groups.google.com/group/sammyjs/browse_thread/thread/2f6124260e4ee4cf http://groups.google.com/group/sammyjs/browse_thread/thread/...
- drivebyacct2 16y agoWhy use hash tags? HTML5 has support for altering the URL without leaving the page and adding new entries in the browser history. Sounds like a better than the age-old hashtag workaround.
- CrabDude 16y agoAgreed, this is what Facebook does, http://www.facebook.com/notes/facebook-engineering/using-html5-today/438532093919 http://www.facebook.com/notes/facebook-engineering/using-htm...: "[Facebook] implemented the History API to enable selective loading of page content via AJAX while preserving readable URLs."