Ember refuses to refresh the template when I make changes to the array field in the model by adding or removing an object

My model includes a field with an array of objects structured like this:

App.Post = DS.Model.extend({
    ...
    codes: attr(),  
    ...
});

and the 'Codes' object looks like this:

codes: [
{
    code: stuff,
    comment: stuff_1,
    other_things: other_stuff
},
{
    ...
},
{
    ...
}
...
]

I have an add/remove button with actions attached. The functions associated with them are as follows:

add_code_input: function() {
        var codes = this.get('model.codes');
        var self = this;
        var last_code = codes[codes.length-1];

        // Unable to edit due to ember.set error
        last_code.code = 'Add new (please change)';
        last_code.code_type = "";
        last_code.comment = "";

        console.log(last_code);

        codes.push(last_code);

        this.set('model.codes', codes);


        console.log(codes);
},
remove_code_input: function() {
    var codes = this.get('model.codes');
    codes.pop();
    console.log(codes);
    this.set('model.codes', codes);
}

The remove operation works fine, but adding new items does not.

When attempting to update last_code, I receive the following error message:

Uncaught Error: Assertion Failed: You must use Ember.set() to access this property (of [object Object])

I am trying to insert a dummy object that users can modify.

The first issue is correctly inserting dummy objects into the array, and the second is updating the template when the model changes.

Answer №1

For efficient array manipulation in Ember, it is recommended to use arr.pushObject(obj) and arr.popObject(). Think of them as the setter and getter functions for arrays.

The attr() function behaves similar to a DS record when working with codes.

If you are dealing with a record, you can simply use record.set('foo', 'bar'). If it's just a plain old JavaScript object, then employ Ember.set(obj, 'foo', 'bar').

When using an ObjectController, implementing these actions should be straightforward:


var newCode = {
  code: 'foo'
};
this.get('codes').pushObject(newCode);

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

Error: The value for 'prepareStyles' property is undefined and cannot be read

Can anyone provide some guidance on this code snippet? I am struggling to understand the error message I am receiving. import React, {PropTypes} from 'react'; import TransactionListRow from './TransactionListRow'; import {Table, TableB ...

Add HTML to the nearest div containing a specific class when a button is clicked using JavaScript and jQuery

Here is the HTML setup I'm working with: <div class='form-row'> <div class='col-2'> Blah Blah </div> <div class='col-4'> <button type='button' class=' ...

The requested resource does not have an 'Access-Control-Allow-Origin' header. This means that access from 'http://localhost:4200' is not permitted

I am trying to make a call to a Spring REST web service from an Angular HTTP service using the get method. However, I am encountering the following exception: Failed to load : Response to preflight request doesn't pass access control check: No ' ...

What is the best way to handle POST parameters in express.js?

I'm trying to figure out how to retrieve an array containing all parameters sent in an HTTP request. After consulting the documentation at http://expressjs.com/4x/api.html#req.params I understand that using req.params and req.query, params will refe ...

Adding form data to a json file using Ajax without the need for php

After spending countless hours attempting to make this work, I'm at a loss. My goal is to append the Title and Content text to a .json file. While I've come across similar issues involving PHP on other platforms, my teacher has forbidden its use. ...

How to iterate through the elements of an object within an array using Vue.js and TypeScript

There was an issue with rendering the form due to a TypeError: Cannot read properties of undefined (reading '0'). This error occurred at line 190 in the code for form1.vue. The error is also caught as a promise rejection. Error Occurred <inpu ...

Moving elements in an array while iterating

I am working on an algorithm that involves shifting the elements of an array n times and returning a new array with each shift. For example, starting with [1,2,3,4], the desired output should be: [[ 1, 2, 3, 4 ], [ 2, 3, 4, 1 ], [ 3, 4, 1, 2 ], [ 4, 1, 2 ...

Trouble displaying JSON data with AngularJS $http service

Struggling to retrieve json data. Initially, I created an ajax request that functioned properly in a regular html page but failed in my angular app. As a result, I decided to experiment with the built-in $http get function. Surprisingly, no errors are thro ...

xhr.send(params) allows for sending multiple parameters in one request

Hey there, I'm looking for guidance on passing multiple parameters with the correct syntax. Can someone please assist me? function formSubmit() { var name = document.getElementById('name').value; var email = document.getElementById ...

Create an element that can be dragged without the need to specify its position as absolute

I am having an issue where elements I want to drag on a canvas are not appearing next to each other as expected. Instead, they are overlapping: To make these elements draggable, I am utilizing the dragElement(element) function from https://www.w3schools.c ...

Querying the database for information using ASP.NET

I am facing a challenge in my ASP.NET MVC application related to the signup form. I need to verify user-entered data against my database table and retrieve the corresponding Id if there is a match. The information collected from users includes their email ...

Next-Auth: What is the process for managing registration using an email and password credential provider?

How does the custom credential provider handle registration with email and password? My current [nextauth].js file looks like this: import NextAuth from 'next-auth' import Providers from 'next-auth/providers' import axios from 'ax ...

Arrange elements in a vertical flow based on the height of the container

I am attempting to alter the direction of Elements to be vertical. Here is an example: By default, HTML elements are displayed horizontally like this:- #container { position: absolute; width: 400px; height: 200px; border: 1px solid gree ...

Utilize the fetch function within a React functional component

I have been experimenting with different methods to fetch data only once before rendering, but I am encountering some challenges: It is not possible to call dispatch in componentDidMount as there is a restriction that it can only be done in Functional c ...

Best practices for DOM manipulation in Angular 2 and beyond

I've come across similar questions, but none seem to address my specific query. So, what is the correct way to manipulate the DOM in Angular? Let's say I have this: HTML <button id="buy-now">BUY NOW</button> If you were using pure ...

unable to utilize the clear method on password field using Selenium WebDriver

I am currently testing the validation on a login form using Selenium webdriver. I have noticed that while the username field clears successfully, the password field does not clear when using the clear method. Is there something I am overlooking? it(' ...

I'm having trouble getting the data to parse correctly in ajax. Can anyone explain why it's not working?

I have implemented a system where an excel file is uploaded using AJAX, and information about the success or failure of the upload is sent from the upload page to the PHP page using json_encode. However, I am facing an issue with accessing this data indivi ...

Tips for stopping a webpage from automatically refreshing

Is there a way to capture the refresh event using javascript/jquery? My entire page is loaded via ajax, meaning all links and forms are sent through ajax requests. However, when a user refreshes the page, it disrupts the functionality I have implemented wi ...

Problem with repetitive looping of Jquery click event within an Angular controller

Currently, I am in the process of creating an app using onsen UI (angular based) + phonegap. One issue I am facing is related to a JQuery click event inside an angular controller. The problem arises when I navigate back through the page stack and then ret ...

How can you insert a title or key into an array containing numerous objects using javascript?

I have a function that extracts data from files and returns it in an array const extractTestCases = () => { const filesArray = [] const filterTestSuite = files.filter(test => test.includes('.test.ts')) filterTestSuite.forEach(te ...