The Concept of Inheritance in Ember.js Models and Utilizing Templates

I'm facing a challenge displaying two different models on the index template in my Ember application.

There are two models, Cats and Dogs, that I want to showcase under the index route. It seems like a simple task, but I am struggling to implement it successfully.

Any suggestions from experienced developers?

Here is the JavaScript code for my application:

window.App = Ember.Application.create();

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.Pet = DS.Model.extend({
  name: DS.attr('string'),
  type: DS.attr('string')
});

App.Cat = App.Pet.extend({
  miceKilled: DS.attr()
});

App.Dog = App.Pet.extend({
  catsKilled: DS.attr()
});

App.Router.map(function() {
  this.route('cat');
  this.route('dog');
});

App.CatRoute = Ember.Route.extend({
  model: function() {
      return this.store.find('cat');
  },
  setupController: function(controller, model) {
    controller.set('model', model);
  }
});

App.DogRoute = Ember.Route.extend({
  model: function() {
      return this.store.find('dog');
  },
  setupController: function(controller, model) {
    controller.set('model', model);
  }
});

And here is the HTML code snippet:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Testing</title>     
  </head>

  <body>
    <script type="text/x-handlebars">
      {{#link-to 'index'}}Home{{/link-to}}      
      {{#link-to 'cat'}}Cats{{/link-to}}
      {{#link-to 'dog'}}Dogs{{/link-to}}

      {{outlet}} 
      {{outlet dog_outlet}} 
      {{outlet cat_outlet}}       
    </script>

    <script type="text/x-handlebars" data-template-name="dog">
      Dogs
      {{#each}}
        <div>
        This dogs name is {{name}}, and he's killed {{catsKilled}} cats!
        </div>
      {{/each}}      
    </script>

     <script type="text/x-handlebars" data-template-name="cat">
      Cats
      {{#each}}
        <div>
        This cats name is {{name}}, and he's killed {{miceKilled}} mice!
        </div>
      {{/each}}      
    </script>    

    <script type="text/x-handlebars" data-template-name="test">  
      Testing  
      {{controller}}
    </script>

    <script src="libs/jquery-1.10.2.min.js"></script>
    <script src="libs/handlebars-1.0.0.js"></script>
    <script src="libs/ember.js"></script>
    <script src="libs/ember-data.js"></script>
    <script src="custom/ember/app2.js"></script>
  </body>
</html>

Answer №1

In your specific situation, it's important to take a step back and analyze the index route. Without any model provided, no data is being fetched from the server. The routes are only accessed when the associated URL is hit, so rendering the dog's controller and template doesn't trigger the cat's route for the model.

However, if you do want to utilize both cats and dogs information in the index route, you'll need to fetch them both. This is where RSVP hash comes in handy: https://github.com/tildeio/rsvp.js/blob/master/README.md#hash-of-promises

App.IndexRoute = Em.Route.extend({
  model: function(){
   return Ember.RSVP.hash({
     dogs: this.store.find('dog'),
     cats: this.store.find('cat')
   });
  }
});

Instead of overriding the renderTemplate hook, you can simply render in the template and provide the template name and context.

<script type="text/x-handlebars" data-template-name="index">
  {{render 'dog' dogs}} 
  {{render 'cat' cats}} 
</script>

It seems like Peter is working the hardest while the other two are slacking off. Maybe he deserves a treat!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interactive window allowing the user to closely examine code

Hey guys, I need your help with something Is there a way (PHP / jQuery) that allows me to zoom in on my code? Similar to how lightbox works for images. I specifically want to zoom in on dynamic code while using Yii's CListView. I'm thinking of ...

The paper.Tool feature is currently experiencing issues when used across multiple canvases

I am encountering an issue with two canvas elements - the first one works fine, but the second one is causing problems with the paper.Tool functionality. Sometimes the onMouseMove event works correctly, while other times it does not. var dataLoad; var myp ...

Node functions continue to run smoothly without affecting the loop

I am working on a webdriverjs application and I am trying to determine when jQuery has finished its processes on the page. I have implemented some methods, but it seems that even when the condition is supposed to trigger an else statement and stop the loop ...

Having trouble accessing a DOM element within a Vue component while using vanilla JavaScript

I am attempting to dynamically update data from a Vue component using jQuery in a Webpack project. Below is the code snippet: <template> <div id="container"> <slot>show string!!</slot> <div id="s_container"&g ...

A step-by-step guide on incorporating an environment map onto a gltf object

I am struggling to add an environment map to a loaded GLTF/GLB file. Currently, I am getting a reflection instead of the desired effect where there should be a black dot with a light point on it. After reading some of the documentation for Three.js, I bel ...

What is the process for creating a server-side API call?

I've designed a front-end application that uses an API to retrieve data. The problem I'm facing is that in order to access the API, I need to use an API Key. If I include the API key in the client-side code, it will be visible to users. How can I ...

Implementing conditional button visibility in Angular based on user authorization levels

I've been experimenting with the following code snippet: <button ng-if="!isAuthenticated()" ng-click="deleteReview()">Delete</button> In my JavaScript, I have: $scope.isAuthenticated = function() { $http.get("api/user ...

Pull data from another domain's DIV using jQuery's load/ajax function

I need to load content from a different domain into a DIV on my JSP page. For example: $("#myDiv").load("https://www.google.com") The issue I'm facing is that the request is being blocked by the browser's same origin policy. I've explore ...

Personalize the error message for throwing an exception in JavaScript

I've been working on customizing error messages for exceptions thrown in JavaScript. Despite my best efforts, I have not been successful so far. I'm currently attempting the following code snippet but it's not functioning as expected: f ...

Investigate the presence of a vertical scrollbar using JQuery or JavaScript

Within an HTML report, I have applied the style "overflow:auto" to allow for dynamic data filling. I am now facing the challenge of detecting whether a vertical scrollbar exists within the report. After scouring various forums for solutions, I came across ...

Vue/Vuex - using async dispatch for AJAX requests in multiple components

I am working with vuex and using a store module to load user lists in my app through ajax. After the list is loaded, it doesn't fetch again if it's already present in the vuex store. I have implemented this logic in the main application layout as ...

Using jQuery to loop through a table and retrieve the button value from every row

I have a challenge with dynamically created buttons in a table. My goal is to loop through the table, identify the rows that have a checked checkbox, and retrieve the value of a button within that row. I then need to store these values in an array. Unfortu ...

Loop through an array of buttons using an event listener

My HTML and Javascript code currently have the functionality to show/hide a row of 3 images upon button click. However, I need this feature to work for two additional rows similar to this one. I believe it involves looping through an array of buttons, but ...

Repeating an AJAX request overlooks the success callback function

I am facing a challenge in refreshing an AJAX call when the token expires. Although everything seems to be working fine with the implementation of ajaxSetup, I noticed that the request is not triggering the success callback as expected. $.ajax({ url: ...

What's the reason for text-alignment not functioning properly?

After some testing, I have come up with the following code: .Greetings { width:100%; display:block; text-align:center; font-family: "Times New Roman", Times, serif; font-size:75px; color:#208CB7; } The objective was to center the text on the ...

Unable to show JSON data on the console

I am attempting to retrieve a large array of JSON objects from a remote server. The server-side is based on Node.js + redis. Here is my ajax query: $.ajax({ crossDomain: true, type:"GET", contentType: "application/json", url: "http://****. ...

What is the best way to retrieve information from an external JSON file using Ionic?

I'm currently working on developing an app using the Ionic Framework. My goal is to retrieve data from an external JSON file by providing a token and parameter (1 for premium or 2 for basic). The URL displays the data correctly, but I am struggling ...

Download CSV file directly in Internet Explorer 10 by choosing to open the file instead of saving it on your device

On my server, I have a link available to download a file: <a id="downloadCSVFile" runat="server" href="javascript:void(0)" onclick="parent.document.location = 'CSVFile.csv';">Download</a> I attempted this method as well: <a id=" ...

Looking to incorporate new values without replacing them in react.js and mongoDB

function getFilterQuery() { const completedWorkState = WorkflowStates.findOne({title: 'Completed'}); const inProgressWorkState = WorkflowStates.findOne({title: 'In Progress'}); const identifiedWorkState = WorkflowStates.findOne ...

One way to filter using a single array

Currently testing some angularJS with this particular example http://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_filter <ul> <li ng-repeat="x in names | filter : 'i'"> {{ x }} </li> </ul> Is ther ...