# 在加载顺序之外访问模型

**URL:** <https://meta.discourse.org/t/accessing-model-out-of-load-order/96661>\
**Category:** Development\
**Created:** [2018年九月7日 16:49 UTC](https://meta.discourse.org/t/accessing-model-out-of-load-order/96661 "2018-09-07T16:49:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)\
**Post date:** [2018年九月7日 16:49 UTC](https://meta.discourse.org/t/accessing-model-out-of-load-order/96661/1 "2018-09-07T16:49:11Z")

</div>

Hey all, I’m trying to work through an issue in the `user-private-messages` controller in a theme.

Here’s what I’m trying to do.

- In the `user/messages` template, add content if there are fewer than 5 private messages in the list.  
Example:

```plaintext
{{#if showMessagesCTA}}
   <p>Hey here I am!</p>
{{/if}}

```

- I’m modifying the `user-private-messages` controller to add the `showMessagesCTA` method, as this appears where the template pulls logic from. The data I need is in the `user-topics-list` model, in particular the `topics` array.  
Example:

```plaintext
api.modifyClass("controller:user-private-messages", {
    showMessagesCTA: function() {
        let userTopicsListModel = api.container.lookup("controller:user-topics-list").get('model');
        if (userTopicsListModel && userTopicsListModel.topics){
             return userTopicsListModel.topics.length() < 5;
        } else {
             return false;
        }
    }.property()
});

```

The problem I’m running into is the model always returns `null`. I’m guessing this is a timing issue because the `user-private-messages` controller loads prior to `user-topic-list`. When I log just the `user-topics-list` controller, it shows up but is greyed out. Clicking into it shows all the data, but it’s clearly not accessible in the app.

 ![Clipboard%20-%20September%207%2C%202018%2011-03%20AM](https://global.discourse-cdn.com/meta/original/3X/0/2/02d1f5ab014a5eca636a0a69a15947d3c86cdcb7.png)

Is there a better way to approach accessing this data I’m not accounting for?

---

<div class="post-metadata">

**Author:** ![gdpelican](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gdpelican/32/81308_2.png) [@gdpelican](https://meta.discourse.org/u/gdpelican)\
**Post date:** [2018年九月7日 20:55 UTC](https://meta.discourse.org/t/accessing-model-out-of-load-order/96661/2 "2018-09-07T20:55:45Z")

</div>

Looks like `user-private-messages.js.es6` provides a direct reference to the `userTopicsList` controller, so you probably want to use that to lookup rather than going through the container.

```plaintext
# user-private-messages.js.es6#6
userTopicsList: Ember.inject.controller("user-topics-list"),

```

This also means that you can set a watch on the `property()` so that the computed property updates automatically when the userTopicsList changes

```plaintext
api.modifyClass("controller:user-private-messages", {
  showMessagesCTA() {
    return this.get("userTopicsList.model.topics.length") < 5
  }.property("userTopicsList.model.topics.length")
})

```

Bonus points, I believe `property()` passes its arguments into the method (and if not, using the `@on` decorator does), so you could end up with something like

```plaintext
api.modifyClass("controller:user-private-messages", {
  @on("userTopicsList.model.topics.length")
  showMessagesCTA(length) { return length < 5 }
})

```

---

<div class="post-metadata">

**Author:** ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)\
**Post date:** [2018年九月7日 21:28 UTC](https://meta.discourse.org/t/accessing-model-out-of-load-order/96661/3 "2018-09-07T21:28:26Z")

</div>

These are some good ideas - I’ll give them a go and report back. Thanks sir!

---

<div class="post-metadata">

**Author:** ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)\
**Post date:** [2018年九月12日 13:43 UTC](https://meta.discourse.org/t/accessing-model-out-of-load-order/96661/4 "2018-09-12T13:43:28Z")

</div>

@gdpelican I really appreciate your help with this – your suggestions worked like a charm!
