Model hook syntax in 2.x


#1

I keep seeing examples that look something like:

export default Ember.Route.extend({
  model() {
    return ...
  }
});

rather than:

export default Ember.Route.extend({
  model: function() {
    return ...
  }
});

I presume this is an ES6ism. I’ve looked around a lot but have failed to turn up any info about this change. What i’m most curious about is how one would go about including an actions collection in the first route above.

Also, are there any compelling reasons to not use the old syntax?


#2

Since Ember CLI use Babel as compiler, I would recommend this document to you: https://babeljs.io/docs/learn-es2015/


#3

Thanks. I’ve seen that doc already. What I’m looking for is how to include both model() and actions: {} blocks within a route. So, specific to Ember, iow.


#4

This is ES2015 method definition shorthand syntax:

It’s probably a smaller change you think. Like you showed you can mechanically change all methodName: function() { to metdhodName() {.

As actions hash is not a method, nothing changes there. But inside the actions hash you can again use the shorthand syntax to define the action handler methods if you like.


#5

Thanks. I’ve just done the obvious thing and played around with it myself. No excuse, really, with how easy it is to fire up an example these days.

OK, so it looks really screwy, but it’s also apparent why this was done. Everything’s an object, after all. It’s certainly not the strangest thing from ES6 to look at. Old dog, new tricks. woof woof

@nightire I’ve just reread my reply to you and realised that it seemed a bit curt. I didn’t mean it that way.


#6

No big deal, it is a very simple change to me, I was not so clear what you gonna ask. Anyway, now you know it, fine.