Is it possible to modify a particular entry in Ember Data without initiating any HTTP requests?

Within my Ember.js application, I have a model named emails structured as follows:

import DS from 'ember-data';

export default DS.Model.extend({
    label   : DS.attr('string'),
    primary : DS.attr('string'),
    email   : DS.attr('string')
});

To populate the model with data retrieved from the server, I use the following code snippet:

this.store.createRecord('emails', {
    label: 'Foo',
    primary: true,
    email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01676e6e416360732f626e6c">[email protected]</a>
});

Users have the ability to add new emails to the store through a form. When a new primary email is added, any existing primary email should be switched to false before adding the new record.

I attempted to access all records using this.store.peekAll('emails') in order to update the one with primary: true, but I am unsure of the correct method. How can I achieve this task?

It is worth noting that I am not utilizing the REST Adapter, hence I cannot utilize Ember Data queries as outlined in this guide.

Answer №1

For a quick way to retrieve a specific record, you can use the Ember Data method peekAll along with the Ember.Enumerable method findBy:

var main = this.store.peekAll('email').findBy('main', true);

Answer №2

To narrow down the list of emails based on a specific attribute, you can use a filter method:

emailsList = this.store.peekAll('email');
filteredEmails = emailsList.filter(function(email){
    return email.get('filterByAttribute') === true;
  });
// The filter method will result in an array, ideally with only one item:
selectedEmail = filteredEmails[0];

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 view and use the data stored within this JSON object?

Obtaining information from a straightforward API (). I retrieve the data using getJSON: var police = $.getJSON(queryurl); A console.log on police displays this: https://i.sstatic.net/Kjyz8.png However, I am unable to access the properties within the o ...

What is the process for embedding a NextJS app within a different website?

I'm searching for alternative methods to embed a React/NextJS application within another application. Currently, my solution involves using an iframe and specifying the source as the execution of the React/Nextjs application. I've explored diff ...

What is the meaning of '=>' in typescript/javascript?

I keep coming across lots of '=>' in the code I found on the internet. Could someone please explain it to me as if I were 5 years old? (I'm searching for the specific code, and I'll share it here once I locate it).. Found it: ...

"Ionic offers a seamless integration of both side menu and tabs for enhanced navigation

I am working on implementing a sidemenu and tabs on the same screen in my Ionic app project. Currently, it is almost working, but I need the bottom tabs to be visible at all times while also being able to navigate to other views from the sidemenu without ...

What is the process for indicating the cache directory in npm5 when using the install command?

When using yarn, you can specify the cache folder with yarn --cache-folder [CACHE_FOLDER]. Is there an npm5 alternative to this? One option is to set the cache folder with a separate command: npm config set cache [CACHE_FOLDER]. However, I'm wonderin ...

Dynamic content for tooltips in Angular

Currently, I am facing a challenge where I need to dynamically populate content in a tooltip by executing a function with a parameter. This parameter holds the crucial information required to update the tooltip content. The complexity arises from the fact ...

What is the best way to assign a value to react-select when working with grouped options?

Is there a way to update the value of a react-select component that has grouped options, as discussed in this post? Responding to @Tholle's comment: I didn't mention that I'm using Typescript because it might limit the number of responses. ...

JSON file not found by the system

I am currently working on a basic program that consists of 3 files: 1. An HTML file named index.html 2. A JavaScript file named app.js 3. A JSON dataset called dataset.json I am struggling to make the browser recognize the data in my program. This is ...

Tips to avoid multiple HTTP requests being sent simultaneously

I have a collection of objects that requires triggering asynchronous requests for each object. However, I want to limit the number of simultaneous requests running at once. Additionally, it would be beneficial to have a single point of synchronization afte ...

Are the JQuery appended elements exceeding the width of the parent div?

I have an HTML div that is styled using the Bootstrap class span9. <div class="span9"> <div id = "selectedtags" class="well"> </div> <button class="btn" type="button" id="delegatecontent">Generate report</button&g ...

Struggling to verify credentials on CouchDB

I'm currently facing an issue with authentication while trying to access a couch server from a web application served on port 80. The credentials seem to be removed when passing them in the URL and the session cookie isn't sent to the server. He ...

Column alignment issue detected

Can you help me with aligning the data in my column status properly? Whenever I update the data, it doesn't align correctly as shown in the first image. https://i.stack.imgur.com/300Qt.png https://i.stack.imgur.com/4Dcyw.png $('#btn_edit' ...

If the user inputs any text, Jquery will respond accordingly; otherwise,

I have a text field that is populated from a database, but if a user decides to change the value, I need to use that new value in a calculation. $('#ecost input.ecost').keyup(function(){ if (!isNaN(this.value) && this.value.length != ...

Encountering an uncaught error event while using Yeoman

Whenever I try to run Yeoman, I encounter the following error: events.js:72 throw er; // Unhandled 'error' event ^ Error: spawn ENOENT at errnoException (child_process.js:1001:11) at Process.ChildProcess._handle.one ...

Is there a better way to implement an inArray function in JavaScript than using join and match?

Recently, I created an inArray function for JavaScript which seems to be working fine. It's short and a bit unusual, but I have a nagging feeling that there might be something wrong with it, although I can't quite pinpoint what it is: Array.prot ...

How can URL parameters be connected to context in a React portfolio website?

I have a compact portfolio site created with React that showcases my work as both a graphic designer and an aspiring web developer. Upon arrival, visitors encounter a landing page where they can choose to explore either the "design" or "web" sections, sett ...

Comparison of WebAPI Response Codes: Understanding the Difference Between 401 and

As a part of my learning project, I am developing a WebAPI and striving to implement best practices. The initial focus is on creating an authentication API that accepts an authentication object in JSON format: { username: myusername, password: mypa ...

Direct users from one path to another in Express framework

I have two main routes set up in nodejs. First is the users.js route: router.post('/users/login', function(request, response) { // Logic for user login // Redirect to dashboard in dashboard.js file after login response.redirect(&ap ...

Initializing an Express app with JSON file loading

My movie-finding application relies on backend API calls to function. As part of the initialization process, I need to load two JSON files: one containing a list of languages for searching (lang.json) and another stored in the config variable that provides ...

JavaScript: Responding to the fetch response based on certain conditions

I am currently working with a fetch() request that can either return a zip file (blob) or a JSON object in case of an error. The existing code successfully handles the zip file by sending it to the user's Downloads folder. However, when a JSON respons ...