Challenging questions about Ember.js: managing save and delete operations for hasMany relationships

Currently, I am working on my first Ember project which is a quiz generator. I have successfully implemented the functionality to create, edit, and delete quizzes, and save them to local storage. However, I am facing challenges in saving/deleting quiz questions for each quiz.

I am developing this project in Yeoman-Ember. I attempted to showcase a demo on JSBin, but encountered some issues. As an alternative, you can access a demo build here: and download the current state of the build in a zip file from this link:

Below is the content of my combined-scripts.js file:

(function() {

var Quizmaker = window.Quizmaker = Ember.Application.create();

/* Order and include as you please. */


})();

// The JS code continues... (The original script continues from here.)

If you have any suggestions on how I can enhance the functionality and resolve the issues with saving/deleting quiz questions, your input would be greatly appreciated. Additionally, I am seeking guidance on how to automatically toggle the isEditing state based on the display of the /edit route, rather than triggering it through an action.

Answer №1

Let's break this concept into two separate sections, starting with the most recent.

An effective way to differentiate between the edit mode and view mode is to utilize the built-in index route, which is displayed at the root level of each resource.

To achieve this, you can modify your quiz template by turning it into an outlet.

{{outlet}}

Additionally, create a quiz/index template that initially contained your quiz data. This template will only be visible when you access /quiz/123. When you navigate to /quiz/123/edit, the index template will be replaced by the edit template. (Remember to pass the model from the resource to both routes using modelFor).

App.Router.map(function() {
  this.resource('foo', {path:'/'},function(){
    this.route('edit');
  })
});

App.FooRoute = Em.Route.extend({
  model: function() {
    return [{color:'red'}, {color:'yellow'},{color: 'blue'}];
  }
});

App.FooIndexRoute = Ember.Route.extend({
  model: function() {
    return this.modelFor('foo');
  }
});


App.FooEditRoute = Ember.Route.extend({
  model: function() {
    return this.modelFor('foo');
  }
});

When working with Ember Data and having two record types with a relation to each other:

App.FooRecord = DS.Record.extend({
  bars: DS.hasMany('bar')
});

App.BarRecord = DS.Record.extend({
  foo: DS.belongsTo('foo')
});

To establish associations between two records:

var foo = store.create('foo');

var bar = store.create('bar');

foo.get('bars').pushObject(bar);

bar.set('foo', foo);

Upon saving the records, here is how Ember Data handles it:

foo.save();
// json sent
{
  foo: {}
}
// response with id 1

bar.save();
// json sent
{
  bar: {
    foo: 1
  }
}

Ember Data opts not to save the hasMany relationship if there is a corresponding belongsTo association from a different model.

https://github.com/emberjs/data/commit/7f752ad15eb9b9454e3da3f4e0b8c487cdc70ff0#commitcomment-6078838

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

What is the best way to utilize "require" dynamically in JavaScript?

Within the "sample.js" file, there is a JavaScript function structured as follows: var mapDict = { '100': 'test_100.js', '200': 'test_200_API.js', '300': 'test_300_API.js' } function mapAPI() { ...

Creating an expandable discussion area (part II)

After checking out this query that was posted earlier, I am interested in implementing a similar feature using AJAX to load the comment box without having to refresh the entire page. My platform of choice is Google App Engine with Python as the primary lan ...

bootstrap-vue tabs - reveal specific tab content based on URL anchor tag

For my SPA, I am utilizing bootstrap-vue and currently working on a page where nested content needs to be placed within b-tabs. If given a URL with an anchor (e.g. www.mydomain.com/page123#tab-3), the goal is to display the content under Tab 3. Query: Ho ...

The scrolling function halts as soon as the element that was middle-clicked is deleted from the

I am knee-deep in developing my own React virtualization feature and encountering a minor annoyance. When I middle click on an item in the list and start scrolling, the scrolling stops once that item is removed from the DOM. My initial thought was that the ...

Phaser encountering a null window.computedStyle() error in Firefox (while dealing with a hidden Iframe)

I'm experiencing issues with Phaser game-engine games crashing specifically in Firefox. The error that keeps popping up is related to a hidden iframe containing the game, which is hidden due to a pre-game video ad being displayed. I keep getting a ...

Sending state information through props in a Vuex environment

One of the challenges I am facing is how to make a reusable component that can display data from the store. My idea is to pass the name of the store module and property name through props, as shown below: <thingy module="module1" section=" ...

Learn the process of updating database information with AngularJS through a button click

As a newcomer to Angular, I am facing an issue in my mini project. The main goal of the project is to display data from a database. I have successfully set up the view to show current data by making API calls and connecting to the database. However, I am n ...

Merge two arrays of objects in Ramda.js based on their shared ID property

I'm working with two arrays of objects: todos: [ { id: 1, name: 'customerReport', label: 'Report sent to customer' }, { id: 2, name: 'handover', label: 'Handover (in C ...

Capturing the unknown elements in a deeply nested array

I'm trying to create a helper function that will return 0 if an element in a nested array is undefined. The issue I'm facing is that when the first index fails and returns undefined, the function should catch errors at subsequent indexes but it&a ...

Avoid turning negative numbers into NaN by ensuring that you do not divide by zero

When attempting to convert NaN to false, negative numbers are also affected. -0.2|0 //this operation will always result in zero when the number is negative I wanted to perform a bitwise operation quickly and inline, minimizing the number of steps, as ...

developing a fresh node within the jstree framework

Recently, I have been working on creating a node using crrm as shown below $("#TreeDiv").jstree("create", $("#somenode"), "inside", { "data":"new_node" }); This specific function is triggered by a wizard, enabling me to seamlessly create a new node withi ...

How to align the navbar toggle button and list items to the right in Bootstrap 5

I'm struggling with a simple html page that has a fixed navbar at the top. Everything looks great when viewed in full-screen mode, with centered links. However, when the page size is reduced, it collapses to a toggle button on the left side. What I re ...

Issue with Bottle.py: When using AJAX, request.forms.get() is returning NoneType

Having trouble sending JavaScript data to a bottle.py server using AJAX? Despite trying numerous solutions from various sources, none seem to be working. To provide clarity, I'm focusing on the AJAX call code here. Can someone explain why request.for ...

Issue with alignment in the multiselect filter of a React data grid

In React Data Grid, there is a issue where selecting multiple filter options messes up the column headers. Is there a solution to display selected filter options above a line in a dropdown rather than adding them to the column header? The column header siz ...

Enhance Your Text Areas with Vue.js Filtering

Currently, I am working with a textarea that has v-model: <textarea v-model="text"></textarea> I am wondering how to filter this textarea in Vue. My goal is to prevent the inclusion of these HTML quotes: &amp;amp;#039;id&amp;amp;#039 ...

Instructions for removing a class using the onclick event in JavaScript

Is there a way to make it so that pressing a button will display a specific class, and if another button is pressed, the previous class is removed and a new one is shown? Thank you for your assistance. function myFunction() { document.getElementById ...

Focusing on pinpointing certain mistakes within Drupal 7

When working with Drupal, encountering errors is a common issue. While some errors are simple to fix, others can be quite complex and require significant time and effort to resolve, even if the website appears to function normally despite the error. My qu ...

Saving an Axios request array to a Laravel controller in Laravel using VueJs

I am working on a laravel 5.7 application that utilizes VueJS 2.0 for the front end. The issue I am facing involves two tables with a many-to-many relationship: 'commandes' and 'produits'. When trying to pass data into my 'commande ...

Disabling dates in the second datetimepicker depending on following days selected in the first one

I have implemented the bootstrap date picker and I am using two textboxes for searching by date range. I want the second textbox to display the days after the date selected in the first textbox. Any suggestions would be appreciated. Here is the HTML code: ...

Display a loading dialog when an AJAX request is made

I am working on a Grails application and I would like to implement a visual indicator, such as a modal dialog, to show when an AJAX request is in progress. Currently, I rely on JQuery for all my AJAX requests. While they are triggered using Grails tags at ...