# Loading substates: A simple search scenario

**URL:** <https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348>\
**Category:** Design\
**Created:** [February 18, 2015, 4:45pm UTC](https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348 "2015-02-18T16:45:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeroicEric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/heroiceric/32/18432_2.png) [@HeroicEric](https://discuss.emberjs.com/u/HeroicEric)\
**Post date:** [February 18, 2015, 4:45pm UTC](https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348/1 "2015-02-18T16:45:27Z")

</div>

I’m trying figure out a good pattern to follow when setting up loading substates.

A lot of the time I’m running into situations where a user decides to go to a new route and I want to take them there immediately. I don’t want my template rendering to be delayed until all of the model hook promises have been resolved. I’d rather bring users to the page that they want to see and have the page populated with data as it comes in.

I thought this was something that I could accomplish by taking advantage of [loading substates](http://emberjs.com/guides/routing/loading-and-error-substates/) but I’m not quite sure how to set this up to get the intended effect.

## A simple search scenario

### SearchIndex route

The `SearchIndex` template here is the one with the darker gray background. The search results are coming from the `model` hook in the SearchIndexRoute. The search input is updating the value of a queryParam defined on the `SearchIndexController` that is set to refresh the `model` hook in the route whenever it changes.

[https://dl.dropboxusercontent.com/spa/ibxdnt7kjmsxult/einleeqf.png](https://dl.dropboxusercontent.com/spa/ibxdnt7kjmsxult/einleeqf.png)

### Search when loading

While the `SearchIndexRoute`’s `model` hook’s data is loading, the `search/loading.hbs` template fills the `outlet` in the `search.hbs` template.

[https://dl.dropboxusercontent.com/spa/ibxdnt7kjmsxult/b441jac8.png](https://dl.dropboxusercontent.com/spa/ibxdnt7kjmsxult/b441jac8.png)

### What I want Search to look like when loading

The problem is that I want the user to immediately be presented with the search input when they decide to transition to this route. Further, when they decide to enter a search query into the input, the search input should stay present while the `model` hook is being refreshed.

[https://dl.dropboxusercontent.com/u/10547667/JS\_Bin\_-\_Collaborative\_JavaScript\_Debugging.png](https://dl.dropboxusercontent.com/u/10547667/JS_Bin_-_Collaborative_JavaScript_Debugging.png)

### JS Bin of the Situation

> **[JS Bin](http://jsbin.com/xasupo/2/edit?html,js,output)**
>
> A live pastebin for HTML, CSS & JavaScript and a range of processors, including SCSS, CoffeeScript, Jade and more...

---

<div class="post-metadata">

**Author:** ![tcjr](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/tcjr/32/14655_2.png) [@tcjr](https://discuss.emberjs.com/u/tcjr)\
**Post date:** [February 19, 2015, 6:18pm UTC](https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348/2 "2015-02-19T18:18:06Z")

</div>

I don’t believe you can use the built-in loading substate for this. I’ve solved this with an additional state flag, like this:

> **[JS Bin](http://jsbin.com/juzutodala/2/edit)**
>
> A live pastebin for HTML, CSS & JavaScript and a range of processors, including SCSS, CoffeeScript, Jade and more...

Also, depending on your user interface requirements, you might prefer to bind the loading state to a class instead of a replacement template, like this: [JS Bin - Collaborative JavaScript Debugging](http://jsbin.com/sociwozewo/2/edit)

---

<div class="post-metadata">

**Author:** ![HeroicEric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/heroiceric/32/18432_2.png) [@HeroicEric](https://discuss.emberjs.com/u/HeroicEric)\
**Post date:** [February 19, 2015, 6:43pm UTC](https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348/3 "2015-02-19T18:43:17Z")

</div>

Thanks @tcjr. Those solutions can be used to get around waiting for the `model` promises to resolve after the initial transition but they don’t solve the problem when the user first navigates to the page. I’d like for the UI around the results to be presented immediately, eventually loading the results when the `model` promises resolve.

---

<div class="post-metadata">

**Author:** ![mdentremont](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mdentremont/32/12116_2.png) [@mdentremont](https://discuss.emberjs.com/u/mdentremont)\
**Post date:** [March 7, 2016, 1:49pm UTC](https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348/4 "2016-03-07T13:49:30Z")

</div>

@HeroicEric I am running into the same situation here.

I am trying to solve it in the following way but there’s a bunch of edge cases which are making it difficult:

- Parent route containing only the search bar, and an outlet. This route has a “search” query parameter which will refresh the model on change, but the route doesn’t have a model
- Child “loading” route with just a loading indicator
- Child “results” route, shares the parent’s controller in order to have access to the “search” query parameter.

---

<div class="post-metadata">

**Author:** ![HeroicEric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/heroiceric/32/18432_2.png) [@HeroicEric](https://discuss.emberjs.com/u/HeroicEric)\
**Post date:** [March 13, 2016, 9:54pm UTC](https://discuss.emberjs.com/t/loading-substates-a-simple-search-scenario/7348/5 "2016-03-13T21:54:18Z")

</div>

@mdentremont You might want to check out [GitHub - danmcclain/ember-deferred-content](https://github.com/danmcclain/ember-deferred-content), which really simplifies cases like this.
