且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

this.get('model')和modelFor之间的区别

更新时间:2023-12-04 11:30:58

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 $返回的子属性。 c $ c>钩子( 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.