Why are routes and controllers classes and not objects?

匆匆过客 提交于 2020-01-07 05:51:07

问题


Both routes and controllers are singleton classes, so why not just do Ember.Route.create() or Ember.Controller.create() ?


回答1:


This is only a guess, but I would imagine because this way is more versatile. Yes, controllers and routes and singletons now, but they might not always be. If Ember decides tomorrow that controllers no longer need to be singletons, you don't have to change the way you've declared your controllers. (I think there might even be a way in the container to make it so your controllers and routes are not singletons. I wouldn't recommend it though.)

There's also a few other smaller benefits. You can extend classes but not objects, so declaring them as classes allows code reuse. Also, you can't declare computed properties at creation time, so you'd have to do Ember.Controller.extend().create() anyway.




回答2:


Whenever we define a new route or controller then we are basically creating a new instance of that route or controller. We do it using create method, so that we can override any method hooks if required. So in short for each route and controller we have different instances( not a single instance). In case of Ember application we use create method because we can have only one application instance, but we can have more than one routes , controllers, views, so we use extend method .



来源:https://stackoverflow.com/questions/28672870/why-are-routes-and-controllers-classes-and-not-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!