Ember.js: My fixtures array is getting cleared out based on the way it is being accessed

I have 2 different controllers in my application. The first one, KeywordsIndexController, is defined as follows:

App.KeywordsIndexController = Em.ArrayController.extend({

    contentBinding: "App.Keyword.FIXTURES"

});

The second controller, KeywordsNewController, is simply an empty arraycontroller:

App.KeywordsNewController = Em.Controller.extend({
});

There are also 2 views, KeywordsIndexView and KeywordsNewView, each with their own specific functions. KeywordsIndexController handles the task of displaying and deleting keywords, while KeywordsNewController takes care of adding a new keyword and then navigating back to the list of keywords (index). Here is how they are implemented:

App.KeywordsIndexView = Em.View.extend({

    templateName: 'templates/keywords/index',

    delKeyword: function(e){
        var obj = App.Keyword.FIXTURES.findProperty("name", e._data.attributes.name);
            App.Keyword.FIXTURES.removeObject(obj);
        }

});

----------------------------------------

App.KeywordsNewView = Em.View.extend({

    templateName: 'templates/keywords/new',

    addKeyword: function(){
        App.Keyword.FIXTURES.pushObject({

            id: App.Keyword.length,
            name: newKey.value,
            scode: '55678',
            campaign: null

        });
        this.get('controller.target.router').transitionTo('keywords.index');
    }

});

THE ISSUE

Although these events work fine individually, there is a problem when trying to add a new keyword after visiting the list page first. This results in errors such as:

Uncaught TypeError: Cannot call method 'hasOwnProperty' of undefined
Uncaught TypeError: Cannot read property 'name' of undefined

Furthermore, when attempting to print out the App.Keyword.FIXTURES array, it appears to be empty.

I am uncertain about what could be causing this issue and would greatly appreciate any thoughts or assistance.

EXTRA CREDIT

In a testing environment like FIXTURES, is there a more efficient way to reference that object instead of using "App.Keyword.FIXTURES"?

Thanks!

Answer №1

When working in a testing environment (referred to as FIXTURES), is there a more efficient way to access that object other than using "App.Keyword.FIXTURES"?

Instead of directly referencing the object, you can use App.Keyword.all() within the model method of your route:

model: function(controller){
    return App.Keyword.all();
}

This array is dynamic and will automatically update if new models are added.

Also, it's recommended not to manually add objects to FIXTURES. Instead, utilize the createRecord method when creating a new object:

App.Keyword.createRecord({
    id: App.Keyword.length,
    name: newKey.value,
    scode: '55678',
    campaign: null
});

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 eliminate a blank array in JavaScript?

After countless hours of searching, I am reaching out for help with an issue that has me stumped. My Node.js application relies on JSON files for project configurations. Using the 'fs' module, I read the JSON file, parse it into a JavaScript obje ...

Uploading custom images with a WordPress widget

I have been occupied with developing my own WordPress Widget, and almost everything is functioning smoothly except for the WordPress media uploader. I have incorporated eight buttons and input text fields to store the URL of the uploaded image. The click ...

Tips for managing MemoryError warnings when working with extremely large arrays for visualizing a function with three variables

Given the function F(x1, x2, x3) with three independent variables x1, x2, and x3 in 1D array format, I intend to plot F (using the contour class) for specific values of x3. Below is a snippet of my code where I suspect an error may be occurring: N = 10000 ...

Using ng-repeat and selectize in AngularJS to populate a multi-select drop-down with values and set selected values

In this instance, I was able to achieve pure HTML select multiple functionality by using this example (JS Bin of pure html select tag). However, instead of sticking to just the pure HTML approach, I opted to use the Selectize plugin. The confusion arose w ...

Tips for utilizing the material ui auto-complete search feature

I am in search of an alternative to material-ui-search-bar because it is no longer being maintained. I have been suggested to try using Material UI's auto complete instead. However, from the examples I've seen, it seems like only text field struc ...

How can you apply a class to a different element by hovering over one element?

Is there a way to darken the rest of the page when a user hovers over the menu bar on my website? I've been playing around with jQuery but can't seem to get it right. Any suggestions? I'm looking to add a class '.darken' to #conte ...

Substituting placeholders within a docx template remains incomplete

Utilizing this specific library, I am implementing a feature to substitute placeholders within a docx template and generate multiple documents. The technologies involved are neutralino and vue for the front-end, where I have devised a method that transmits ...

Is JavaScript's setTimeout 0 feature delaying the rendering of the page?

Based on information from this StackOverflow post The process of changing the DOM occurs synchronously, while rendering the DOM actually takes place after the JavaScript stack has cleared. Furthermore, according to this document from Google, a screen r ...

Stopping the webdriver in Node.js gracefully without crashing it can be achieved through a

Can the selenium webdriver be halted without terminating node? I've encountered a problem attempting to create an API tool that conducts web automation upon receiving a get request. Essentially, I am executing the selenium webdriver on a get request ...

Generate a new JavaScript object and populate it with information

I am looking to create a JavaScript object in the following format: var CarList = {}; I then want to populate it using a for loop (retrieving data from a MySQL database) similar to this: for(var i = 0; i < result.length; i++) { CarList.Construct ...

JavaScript: Various Outputs Depending on Input

There seems to be a discrepancy in the results when I input "coup," "cou," and then "coup." Can anyone offer insight into why this might be happening? To view my code on jsfiddle, click here: jsfiddle.net/xbuppd1r/43 var data = {"cards":[{"$type":"Assets ...

Vue alert - Cannot access indexOf property of a value that is undefined

After browsing through numerous similar issues on StackOverflow, none seem to address the specific problem I encountered... I have been smoothly working on a Vue JS project + Laravel until I suddenly encountered an error that seems unsolvable (even though ...

Issues with sending FormData through Ajax POST requests over a secure HTTPS connection

We are currently experiencing issues with uploading pictures to our server. The process works smoothly on http sites, but encounters errors on https sites. An error message is displayed:https://i.sstatic.net/hPMZv.png Failed to load resource: the server r ...

What is the best way to incorporate my light/dark mode theme into index.js and have my header serve as the switch location?

Could someone assist me with setting up a theme around my index.js components and using a switch state in my header.js to toggle between light and dark themes? I'm looking for a way to simplify the process to minimize the amount of code needed. As a ...

What are the proper methods for accurately testing vuex state and mutations?

Can someone guide me on how to properly test mutations and state? I have a modal window component that is rendered when the showModal property in the state is true. There is an event triggering a mutation that changes this property. How can I verify that a ...

Importing data features into Vega-Lite

My attempt to plot area shapes using vega-lite is resulting in the following warning message. [Warning] Dropping {"type":"geojson"} from channel "shape" since it does not contain any data field, datum, value, or signal. The data is loading successfully, ...

What is the best way to handle the keystroke event in a $.bind method?

I'm struggling with passing a specific keystroke through the bind method. $(document).bind('keyup', event, keyup_handler(event)); This is my attempt at solving it.. Here is the function it should be passed to: var keyup_handler = functio ...

Prevent Jquery .remove event from affecting child elements, only target the parent

<script> $(".alert").click(function(){ $(this).fadeOut(300, function(){ $(this).remove(); }); }); </script> <div class="alert alert-error"> <h4>title</h4> <te ...

Is there a way to utilize JSON data to dynamically fill column names in a JQuery datatable?

JSON Data: { "data": [ { "name": "Garrett Winters", "designation": "Accountant", "salary": "$170,750", }, { "name": "Brielle Williamson", "designation": "Integration Specialist", "salary": "$372,000", } ] } H ...

Changing text content of an element with dynamic formatting

When a link is clicked on, I want to dynamically set the text. Here is the current code: <a href="www.somelinkhere.com" onclick="getElementById('setText').innerHTML='text to replace'" target="someIFrame" ...