this.get('model')和modelFor之间的区别 [英] Difference between this.get('model') and modelFor

查看:116
本文介绍了this.get('model')和modelFor之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是非常陌生的人,并没有真正理解两种语法之间的区别。我应该在哪里和在什么情况下使用一个或另一个。即

I am quite new to ember and don't really get the difference between two types of syntax. Where and in which situations should i use one or another. I.e. Which one is more suitable for usage in Routes and which one for Controllers.

this.get('model')

相对于

this.modelFor('artists/show')


推荐答案

this.get('model') //controller call
this.modelFor('someRoute') //route call

在Ember中,默认情况下,路由 setupController 钩子执行以下一行代码:

In Ember, a routes setupController hook by default performs this one line of code:

setupController: function(controller, model){
   controller.set('model', model);
}

这需要从模型返回的内容钩子,并使用此值设置控制器的 model 属性。在控制器内部, this.get(‘model’)是访问此模型的正确方法。此外,开发人员可以覆盖此钩子并执行其他操作,例如将 model 设置为等于从 model 钩子( controller.set('model',model.prop)。这值得注意,因为当您调用 this.modelFor 从另一条路线,您不会获得由 setupController 设置的路线的关联控制器模型。 c> model 钩子,如果我没记错的话,它是路由的 currentModel 属性。

This takes whatever is returned from the model hook and sets the controller's model property with this value. From within the controller, this.get('model') is the proper way to access this model. Also, a developer can override this hook and do something different, like set model equal to some child property of what is returned from the model hook (controller.set('model', model.prop). This is worth noting, because when you call this.modelFor from another route, you DO NOT get the route's associated controller's model that is set by setupController. You get whatever is returned from the model hook, which under the covers is the route's currentModel property if I remember correctly.

这篇关于this.get('model')和modelFor之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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