A guide on configuring multiple controllers within a single route in ember.js

I am new to working with ember.js and currently dealing with two models - User and Role

App.User = DS.Model.extend({
    name: DS.attr('string'),
    roles: DS.hasMany('role')
});

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

My goal is to assign or remove roles for a specific user in my application. To achieve this, I need to compare the roles associated with the user against all available roles by looping through and comparing two controllers.

When displaying the assigned roles to a user in the user/edit template, I use the following code:

Assigned Roles: {{#each role in roles}}*{{role.name}}{{/each}}

However, I also need a RolesController in the same route that will display all available roles regardless of the user. Setting up this controller in UserEditRoute may cause conflicts due to naming conventions.

The objective is to list all roles with checkboxes, where the roles assigned to the user are pre-checked and others remain unchecked.

Check out this jsfiddle link

Answer №1

If you want to instruct Ember on how to connect the User controller with the roles controller, you can utilize the needs property in the UsersEditController.

Make sure to specify the model for this controller by implementing the setupController hook within the UsersEditRoute. This way, you can display your list of roles.

App.UsersEditRoute = Ember.Route.extend({
  setupController: function(controller,model) {
        controller.set('model', model);//UsersEditController
        this.controllerFor('roles').set('model',this.store.find('role'));
  }
});

Controllers

App.UsersEditController = Em.ObjectController.extend({
    needs: "roles",
    rolesController: Ember.computed.alias("controllers.roles")
});
App.RolesController = Em.ArrayController.extend({});

Lastly, here's the template:

  <script type="text/x-handlebars" data-template-name="users/edit">
    <h3>User Edit</h3>
      <p>Name: {{name}}</p>
      <p>Assigned Roles: {{#each userRole in roles}}&nbsp;*{{userRole.name}}{{/each}}</p>
      <p>Available Roles:{{#each role in rolesController}}&nbsp;*{{role.name}}{{/each}}</p>
  </script>

Check out the code on jsfiddle here

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

stop automatic resizing of windows

My CSS is written using percentages. I need to maintain the percentages intact. Is there a way to stop the layout from changing when the browser window is resized? I want the percentages to remain unaffected, even when the browser window is being resized ...

Organize an array of objects based on another array in JavaScript

I have been working on sorting an array of objects by comparing it with another object. The goal is to display the selected objects at the top and the rest below them. While I am currently achieving the desired output, I am interested in exploring ways to ...

After reloading the data tables, analyze the information and dynamically highlight any rows in red that remain unchanged

I have a table that is refreshed every 5 minutes using ajax.reload(). One of the rows in this table is labeled as CIP. My goal is to highlight the CIP row on each refresh where the value remains unchanged from the previous value (value received in the la ...

One way to display GIFs sequentially without preloading them is by using JavaScript

On my website, I'm trying to display five animated gifs sequentially. I want gif2 to appear and start its animation only after gif1 has finished its animation. However, the code I'm using is giving me some unexpected behavior. While it works corr ...

Should I use an array literal or split a string?

Imagine you are in need of a predetermined list of words (the focus here is not on the debate surrounding hard-coding). Would you choose this approach: var items = 'apple banana cherry'.split(' '); Or do you favor this alternative: ...

What is the best way to convert my Chatbot component into a <script> tag for seamless integration into any website using React.js?

I have successfully integrated a Chatbot component into my Next.js application. https://i.stack.imgur.com/BxgWV.png Now, I want to make this component available for anyone to use on their own website by simply adding a tag. My initial approach was to cre ...

Is there a way to prevent a button from being clicked until a certain callback function is triggered

I am struggling with configuring my contact form to disable a button when the form is not valid or a recaptcha is not selected. <form name="contactForm" data-ng-controller="ContactCtrl" novalidate> ... <input class="form-control" name="email" typ ...

What is the method for retrieving the active element with Waypoint?

Currently, I am implementing Waypoint (version 7.3.2) in my React project using React version 16. My goal is to create a scrollable list of items where each item fades out as it reaches the top of the container div. My main inquiry is how can I obtain a re ...

Learn the process of seamlessly uploading various document formats, videos, and previewing documents with Angular software

I am having trouble viewing uploaded files in the carousel. While I can see video and image files, other document formats are not displaying. Can someone please recommend a solution to enable viewing all types of documents as well? mydata = [] onSelect ...

The GET request for a "places" endpoint in a node js application is experiencing issues

Today marks my second day working with node js and mongo. I've explored different tutorials and currently, I'm attempting to follow this specific one: https://github.com/amejiarosario/todoAPIjs/commit/d3be6a287e8aff39ab862971da4f050d04e552a ...

Modifying the onclick function for a bootstrap glyphicon

How can I swap the .glyphicon-menu-down to glyphicon-menu-up when a user clicks on a link? Currently, it's not working as expected. Could there be an issue with my jQuery implementation? Markup <a data-toggle="collapse" data-parent="#accordion" h ...

Creating a recursive function using NodeJS

This particular challenge I am tackling is quite intricate. My objective is to develop a recursive function in NodeJS that can interact with the database to retrieve results. Based on the retrieved data, the function should then recursively call itself. F ...

Implement the switch case statement within a node.js application

When it comes to implementing a switch case for registration flow, there are various options available. For instance, you can have the user sign up using their email, phone number, and password; or they can opt for signing in with Google, Facebook, and man ...

Javascript navigating through the User control

Our current folder arrangement looks like this: ParentFolder HostPage1.aspx UserControlsFolder UserControl.ascx AnotherFolder HostPage2.aspx The UserControl.ascx file is utilized in both HostPage1.aspx AND HostPage2.aspx I have an e ...

Invoking a function of a Redux-form React component

Is there a way to access the component method of a redux-form? I want my upload button to submit the form and also open the file select dialog if the user hasn't selected any file. Here is my code: UploadModal.js import React from 'react&apos ...

I'm having trouble getting the animation to work with jQuery. Despite trying everything, the animation doesn't seem to respond to any button clicks

Issue: I am facing a problem where nothing happens when I click the menu on the HTML page. It seems like a total beginner mistake, but I can't seem to get it to work. I have checked the code, and alert works fine, indicating that jQuery is connected t ...

How can we enable Material UI tooltip based on certain conditions?

In my React component utilizing Material UI, I have the code snippet below: const MyButton = ({ warningText }) => ( <Tooltip title={warningText}> <Button>Do action</Button> </Tooltip> ) At present, an empty tool ...

I am trying to set up an AJAX call in my index.html to connect to a React endpoint, but instead of accessing the intended page, I am getting

I am attempting to execute an AJAX call in my static file index.html to a React endpoint, but instead of the desired response, I am seeing the React homepage. Here is the code snippet from my index.html: <div id="data"></div> <scr ...

Creating a highly innovative server infrastructure tailored for Dojo applications with exceptional features

Recently, I have been using web2py for my projects and have found it incredibly useful in creating RESTful web applications. However, I am now looking to expand my skills in JavaScript by developing a more modern and dynamic client-side application that re ...

Enhance the original array of a recursive treeview in VueJS

After going through this tutorial, I decided to create my own tree view using recursive components in Vue.js. The structure of the input array is as follows: let tree = { label: 'root', nodes: [ { label: 'item1', nod ...