如何访问对象控制器中的ember模型(“init”) [英] How to access ember model in on("init") in object controller

查看:102
本文介绍了如何访问对象控制器中的ember模型(“init”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在对象控制器中访问on(init)中的ember模型?在这种情况下,它是未定义的:

How to access ember model in on("init") in object controller? Its undefined in this case:

  inititalise: function () {

    // this.model is undefined

  }.on("init"),


推荐答案

简单的答案是你不能。在路由有可能在控制器上设置模型之前,控制器由容器实例化。如果此对象控制器是由Ember为您自动设置的控制器,则在初始化期间无法访问该模型。尝试观察模型属性。

The short answer is that you can't. Controllers are instantiated by the container before the route has had a chance to set the model on the controller. If this object controller is a controller set up automatically for you by Ember, you're not going to be able to access the model during initialization. Try observing the model property instead.

我在这个指南中找不到任何文档,所以我会链接你到源代码。如果您阅读 setup 函数的源代码,那么您将看到 generateController()被调用,然后 setupController()稍后调用。实质上,创建控制器,Ember做一些工作,然后设置模型。 Ember不会在创建控制器时设置模型。

I can't really find any documentation in the guides on this, so I'll link you to the source code. If you read the source for the setup function, you'll see that generateController() is called and then setupController() is called some time later. Essentially, the controller is created, Ember does some work, then the model is set. Ember doesn't set the model for the controller on creation.

这篇关于如何访问对象控制器中的ember模型(“init”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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