127 points · by hpvic03 · 12y ago · 62 comments
The problem with Batman (aside from being buggy) was that it tried to do this same MVC that we use on the server on the client, and the mapping just doesn't make sense. HTTP "MVC" doesn't have state between requests whereas client-side MVC has state for the entirety of the app. Rails, for example, has a Router and Controller due to the synchronous nature of how a request comes in and gets turned into a response. The first place I see MVC JS frameworks fall apart is when they have both Controllers and Routers. Ember Controllers look like little more than Decorators. I imagine these would be called a ViewModel if named appropriately.
Over two years later, I can't help but feel Backbone (minus the underabstracted View layer, which can be easily replaced with React) got the mental model for JavaScript apps right from the start.
Ember just seems to have been marketed much better than Batman was, which is no surprise, Yehuda is good at doing that.
I don't think this is fair. We use Ember at work, although I wish we used React. We started out on Backbone and it was miserable. Ember has gotten popular because it and Angular were seen as the successors to Backbone for a long time. Ember is designed in a much more thoughtful (and I think better) way than Angular.
Yehuda has done a lot of good work on Rails, Bundler, jQuery. That probably helps sell people on his projects. I wouldn't chalk it up to marketing.
Could you expand on why you prefer React? I have not tried React yet, but am interested to hear thoughts from someone who's used both.
You have to remember that MVC is a very, very loosely defined term. The Ember MVC is very, very different than the Rails MVC. Ember's is much closer to Cocoa or Smalltalk in this regard.
To see how these bits fit together in Ember, I highly recommend seeing Tom's talk on URLs and their importance: http://vimeo.com/77760308 (you can skip the first 12:35 or so, and around 30:38 is where it really gets into it.)
For some teams that's Underscore templates, for some it's Handlebars, for some it's in-house widget libraries, for some it's React.
If you remove the Views from Backbone the only thing left of value are the Models and Collections. Are you saying they got those "right"? I really have no idea what that means. On the contrary — I think Backbone got a number of things wrong. Their router especially.
> "Ember just seems to have been marketed much better than Batman was (...)"
You said that Batman was buggy and got MVC wrong. Perhaps Ember is /not/ buggy and actually manages to deal with state quite well?
Easily using localStorage is also common because just about every JS framework uses the Adapter pattern for persistence.
Ember is conceptually pretty massive though. A tutorial like Michael Hartl's Rails tutorial would be a huge benefit, it looks like that's what this is aiming for. Thanks!
A couple of examples of Ember.js-specific gripes though:
- We have a table with one of the filters just stops working after changing the value 4 ~ 5 times. I've dug into this, but I can't really figure it out without going into the Ember.js internals (which I have done before, but the time sink isn't currently worth it). I've boiled it down to the fact that at some point Ember.js stops responding to changes on a particular attribute. Computed properties stop working sooner, but even a .observes('attribute') stops triggering after 5 or 6 changes.
I get that software is not bug-free, but how am I supposed to even debug something like that? It would be fairly difficult (and time-consuming) to boil it down to a simple test case, as this is the only place we're seeing this happen and it's in a large Ember.js application.
- There is no case/switch statement in Handlebars. I'm left with deeply nested if/unless blocks, or tons of computed properties on controllers/views that generate this stuff. E.g.:
{{#if valueLoading}}
Loading...
{{else}}
{{#if valueLoadingFailed}}
Error!
{{else}}
{{#if value.length}}
<ul>
{{#each item in value}}
<li> {{item}} </li>
{{/each}}
</ul>
{{else}}
No Items.
{{/if}}
{{/if}}
{{/if}}
vs. {{#if value.length}}
<ul>
{{#each item in value}}
<li> {{item}} </li>
{{/each}}
</ul>
{{else}}
{{stateMessage}}
{{/if}}
... and on the controller ...
stateMessage: function () {
return this.get('valueLoading') ? 'Loading...'
: this.get('valueLoadingFailed') ? 'Error!'
: this.get('value.length') === 0 ? 'No Items.'
: '';
}.property('valueLoading', 'valueLoadingFailed', 'value.length'),2. The typical pattern is lots of computed properties. They're lazily calculated, so that makes them pretty cheap.
This was a single value (e.g. 'selectedItem') populated by a Ember.Select view/component. After changing the selection a number of times, all things watching 'selectedItem' completely stop firing off (until a page refresh). No idea why.
How do I save the creation of a new invoice (with line items) as a single transaction? As far as I can tell, you can't without breaking from Ember Data's very strict 'this is the way things should be done' model.
You have to create a new Invoice, then create the InvoiceLineItems doing something like this:
var self = this;
self.get('store').createRecord('invoice', {
.. attributes ..
}).save().then(function (invoice) {
return Ember.RSVP.all(self.get('lineItems').map(function (item) {
return self.get('store').createRecord('invoiceLineItem', {
invoice: invoice,
.. attributes ..
}).save();
});
}).then(function (invoiceLineItems) {
// do something
}).then(null, function (error) {
console.error(error);
});
Note that this isn't even taking into account how to rollback INSERTs if, for instance a single invoiceLineItem fails to be created.The 'other' way to do it is to create just an Invoice model, and have a lineItems attribute of type 'raw' and just create a RawTransform that's just as pass-through of whatever the JSON has for that attribute. It works, but at the same time, feels like it's going against the Ember Data Way, especially if you have a need for an InvoiceLineItem to be its own model (i.e. to be able to maintain relationships to other things and look one up by ID via the API).
Then you might have something like:
- Invoice and InvoiceLineItem models
- A RawTransform:
App.RawTransform = DS.Transform.extend({
deserialize: function (data) { return data; },
serialize: function (data) { return data; },
});
- An attribute like so (on Invoice model): lineItems: DS.attr('raw'),
- Code that looks like this: var self = this;
self.get('store').find('invoice', invoiceId).then(function (invoice) {
self.get('store').pushPayload('invoiceLineItem', { invoiceLineItems: invoice.get('lineItems') });
});If you are a RoR person, I'm sure this tutorial is just great. But for the rest of us...
Perhaps I can just extract out the Ember portion -- It should still be applicable regardless of the backend.
It would be much simpler if you keep it entirely client side, using something like Pretender (https://github.com/trek/pretender).
If someone wrote a plain Ember tutorial that just assumed the backend API already existed, just as many people might make the complaint "if you already have a backend, I'm sure this tutorial is just great, but for the rest of us..."
this.get('store').createRecord('ModelName', {
.. attrs ..
}).save().then(function (model) {
// Ember.isNone(model) === true
}, function(error) {
console.error(error);
});
The fix: s/ModelName/modelName/This works sometimes, and not others. The only indication of failure is the fact that the model is not loaded from the JSON response to the POST/PUT request. It's a subtle bug. I wasted a bunch of time tracking this down through the Ember Data internals (for a co-worker, but the original bug/typo could easily have been mine).
The other quirk is converting snake case to camel case and back:
address_1 =(to camelcase)=> address1 =(to snakecase)=> address1
Oops! We only use capital letters to determine where underscores go! ;-)There are lots of good parts to it - that same convention of configuration saves lots of boilerplate, data binding is usually really nice, overal separation of concerns when doing it ember-way is rather good (far from great though), but lots of small nuisances here and there make up for a dubious overall experience.