为什么路由和控制器类而不是对象? [英] Why are routes and controllers classes and not objects?

查看:114
本文介绍了为什么路由和控制器类而不是对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

路由和控制器都是单例类,所以为什么不做Ember.Route.create()或Ember.Controller.create()?

解决方案

这只是一个猜测,但我可以想象,因为这种方式更为通用。是的,控制员和路线和单身人士现在,但它们可能并不总是。如果Ember明天决定控制器不再需要单身人士,那么你不必改变你宣布控制器的方式。 (我认为在容器中甚至可能会有一种方法,使您的控制器和路由不是单例,我不会推荐它。)



还有一些其他较小的好处。您可以扩展类而不是对象,因此声明它们可以允许代码重用。此外,您无法在创建时声明计算属性,因此您必须执行 Ember.Controller.extend()。create()


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

解决方案

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.

这篇关于为什么路由和控制器类而不是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆