311 points · by wycats · 14y ago · 112 comments
As I've discussed before [1], the current discussion about MVC frameworks in JavaScript is very superficial, and meaningful API differences between, e.g. Spine and Backbone, are not sufficiently understood by people who are picking between them.
Yehuda has a lot of experience thinking about framework APIs from the standpoint of modularity, performance, and developer convenience.
He is absolutely right to emphasize that calculated properties and managed attributes on the model layer are the key to easily building JavaScript interfaces. The difference between nice MVC code and poor MVC code or jQuery soup is the idea that in each method data is only flowing one direction: nowhere are you manually updating both a UI and a data object. Two-way data flows make up the majority of the code in non-MVC jQuery-oriented code. In contrast, a robust model layer with lots of events is the best way to get code reuse and composition.
There is also a model binding plugin to Backbone: https://github.com/derickbailey/backbone.modelbinding
The model contains the "truth" -- the current state of the app. Views should be bound to models and automatically update when the model changes.
But when the view changes, the reverse is not the case. You don't want every checkbox click, radio button twiddle, and keystroke to be causing your model to change ... thereby emitting changes which may cause other pieces of your app to re-render, changes which may cause Ajax requests to persist the model state to the server, and so on.
Instead, most apps have a "save" button for some changes, a mouse click for others, or drag and drop, or a gesture, or an "undo" link.
It's much more convenient and flexible to allow your app to react to DOM events as it needs to, instead of having specific model attributes hard-bound to concrete DOM elements. ... that said, if the latter is your cup of tea, there's a plugin for that.
tl;dr: managed attributes and fine-grained model attribute events make life a lot easier and change the boundary between model and controller.
I can't help but feeling that the oh-so-confusing situation with SproutCore just got more confusing. I, like many others, tried to start using SproutCore and found it to be a poorly documented jumble of code. Fortunately, SproutCore 2.0 was going to fix a lot of that. Only now it isn't. Do we have two half-baked, related frameworks? Are they kinda ports of each other? Is SproutCore 1.x now deprecated? And how confusing will it be when SproutCore 1.x upgrades to a not-AmberJS SproutCore 2.x?
This smells of politics/investor-meddling/internal-disagreements/something to me: fresh, innovative, though-derivative take on an existing codebase is forked out of the original company and is moved under another company (Tilde). Something seems amiss.
SproutCore 2.0 becomes Amber. We'll be moving the code into the amberjs organization today. The SproutCore folks, who are now focusing on native-style applications, will be carrying the torch on the (not deprecated) SproutCore 1.x.
I've been an observer of the JavaScript eco-system and it is changing so rapidly, particularly in the MVC space (i.e. SproutCore 1.xx, SproutCore 2.xx, BackBone.js, Spine.js, Knockout.js, etc etc).
It's so easy to get stuck in analysis paralysis. Is there a discussion list or a website that can help make some decisions for my next hack project?
I'm coming from a heavy backend background and I think the JavaScript MVC space warrants a hack-day project :)
SproutCore (1) has a list of apps halfway down this page: http://www.sproutcore.com/
You can scroll through a long list of Backbone apps here: http://backbonejs.org/#examples
I'm not aware of app lists for the others you mentioned.
Repeat using Y.
Repeat using X.
I recently tried out SC2 (now Amber, I suppose) and I had a "Wow this is cool!" moment immediately followed by a "Wow I can't do shit" moment due to lack of documentation combined with the steep learning curve. I had to put the project on hold; I hope the framework gets some serious love in the next couple months.
var prop = model.get('prop')
It seems much more elegant to write: var prop = model.prop()
You can do the computed properties and binding thing the same way, it's really just a small difference in the model API.But really, what I'd love to see is more model-agnostic tools. Why do I have to buy into SproutCore or Backbone's model implementation just to use the other features?
For example, imagine you have an Store object that saves the tax rate:
Store = SC.Object.create({
taxRate: 0.0895
});
Since this is a static value, I don't technically have to use an accessor. But imagine my state implements a tax holiday, and so for one day per year, the tax rate is different. In Amber, this is a simple change: taxRate: function() {
if (this.get('isTaxHoliday')) {
return 0;
}
return 0.0895;
}.property()
Instead of trawling my codebase to references for this property and changing them to method invocations, because everything goes through get(), I know the change will be transparent. It's this transparency that leads to very maintainable web apps.Some people object to the extra method invocation, but I think the expressiveness it gets you is worth it. As soon as proxies land in JavaScript, we'll add support for them so you can use the dot notation and still get the same functionality.
var prop = model.prop()
Note the parens there. I just like giving each accessor a unique method name as opposed to getting at all properties through a single get() method.Additionally, specifying the property as a string allows you to implement "unknown property" handlers, which is another extremely powerful idiom.
Examples:
1) .('MyApp.someObject*someProp');
2) .setPath('gomer.Sumer.firstName', 'Ra')
etc.
Sold! You read my mind there, Yehuda.
I don't exactly know why, but I'm excited by this. Heart beating noticeably faster excited. I'll be playing around with this on the weekend.
i.e.: sproutcore.js/blossom - "desktoppy" MVC framework with HTML5/js as a display layer
amber.js - "web-appy" MVC framework with HTML5/js as a display layer?
*edit:
I don't have a clear idea about what's going on in the SproutCore community, but I don't think this has universal support.
One of the reasons we rebranded to Amber is that our codebase is inspired by SproutCore 1, but it is a complete rewrite, targeting a smaller file size and web-centric development.
All of the fragments of SproutCore 1.x target native-style applications with a ton of JavaScript, while Amber targets ambitious web-style applications using HTML and CSS for the presentation layer. Amber apps don't have much in common with SproutCore 1 (or "blossom") apps, so a clean naming break felt like a clarifying thing to do.
It is, however, API-compatible with the MIT-licensed view layer (called "Classic") in SproutCore.js.
See the paper for more details.
@import "one.js" // loaded in parallel to the other two
@import "two.js" // loaded in parallel to the other two
code code code // this is run only after one.js and two.js (but not three.js) have been loaded and run
@import "three.js" // loaded in parallel to the other twoBasically the only way to get this asynchronously loading / synchronously executing behavior is to statically analyze your "root" module for dependencies and load them (and their dependencies, etc). Once they're all loaded you can begin execution and synchronously execute every module as they're imported.
FYI CommonJS (not AMD) loaders for the browser do the same thing to avoid synchronous XHRs that will cause the UI to hang.
Then when you want to move from development to production it concatenates and minifies all the files for improved network performance.
I'm serving a substantial Amber app this way and it works great. I'm taking it a step further by writing most of my code in Coffeescript, and I keep all my Handlebars templates in separate files as well. Sprockets just magically combines it all.
So there are still core folks working on 1.x, but in different contexts.
It's been clear where SproutCore has need to go since 2009, but there hasn't been the support to do that, due to the various personalities involved. Those people have moved on, and now SproutCore can do what it's good at: building fast, desktop-style applications that run in a web browser.
Things haven't looked as positive for SproutCore in two years. I pumped.
Declarative presentation in HTML/CSS coupled to JS (or better CS) is the right layer of abstraction for building GUI's for most applications in the foreseeable future.
Amber.js looks very interesting, i hope it paves a new wave of client frameworks that concentrate on state sync between client/server (and multiple clients) with some simple binding to presentation.
As far as the "right" abstraction on the web... this too is evolving and highly dependent on your application. Are you curating a collection of hyperlinked documents? Or are you writing a drawing application?
If your answer is closer to the latter than the former, then the analogy you're looking for is something more like Postscript, where HTML and Javascript serve as a display language for an output device (a browser), driven by an underlying application.
I could easily imagine an Amber object representing a File in node, for instance, allowing other objects to bind to properties like mtime, etc.
In my demo, I used socket.io to update an HTML page whenever one of those properties changed.
I like the idea of SproutCore, but I really just want to do <script src="sproutcore.js"> and have it get out of my way from there. YUI seems to be able to do this and it seems even larger than SproutCore, recently including MVC in its "App Framework". Of course, you need to bring your own templating, but even then that's just another JavaScript include.
Or am I missing the point of SproutCore? What is it adding that, say, YUI or Dojo isn't, that it requires so many programs?
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="js/libs/sproutcore-2.0.beta.3.min.js"></script>
<script src="js/app.js"></script>I think Google with GWT and Eclipse less so with RAP had the right idea. Come up with technologies that abstract away the need to touch JavaScript directly. Just Java (or I suppose C# could work) on the client and the server means the developers are more in sync.
I'm more on the back-end so my perspective is more from the side lines, please don't read too much into it.
For those who are unfamiliar: Angular examples are about 1/4 the size of backbone equivalents. I'm working on an angular version of the most recent peepcode backbone cast. It's about 1/5 the code size, and available here: https://github.com/ludicast/angular-peepcode-todo
Can anyone explain the differences between Amber and Backbone, why Amber might require less coding?
Backbone provides a basic but complete set of tools for observing model changes combined with a set of utilities that are useful but not prescriptive to make wiring up DOM events, URL handling, etc straightforward. It works with what's in jQuery/the browser. All together, it provides the organization most js apps are sorely lacking but doesn't dramatically reduce LoC over what you could do with well factored jQuery and underscore. It's nice enough that the YUI team basically adopted it wholesale for the App framework in 3.5.
Amber is all about binding. You can not only observe properties but bind them to other properties bidirectionally so that changing one causes the change to propagate through all bound values including things like values being arrays. This extends to the Handlebars templating (which was written for Amber) where you can bidirectionally bind, for example, a boolean on your model to a checkbox and to a class on a div so that checking the checkbox toggles the class without anything in your app directly manipulating the DOM. The implementation is an attribute plus two object path strings in the template. It's not panacea but since DOM/Event/State manipulation is ~60-70% of most JS apps I've worked with, you can achieve dramatic code size reductions.
The cost is that Amber is 10x the size of Backbone, the templating language is tied to the framework, and there's more overhead in understanding the concepts, how they fit together, and how to apply them to your code. When I talk to people who are writing jQuery spaghetti, I steer them towards Backbone for the simplicity and superior docs but mention that I use Amber for my own projects.