Problem with Ember.js: Inconsistencies in CSS transitions

Currently, I am working with Ember.js using ember-cli and ember-data. The development process has been running smoothly so far, but recently I encountered an issue related to css transitions that I am struggling to resolve on my own.

The problem arises with a list in which each element contains subelements that are also rendered as a list. I am fetching data from a REST API using ember-data. Upon receiving the data, I intend to apply a fade-in effect (via css opacity) to the list. However, the transition does not always work as expected. It seems to be a timing issue. To address this, I incorporated Ember.run.next into my code, but it did not provide the desired solution. Surprisingly, adding a setTimeout with just 1ms within Ember.run.next seemed to resolve the issue, at least on my end. This situation feels quite peculiar. Below is the code snippet I have thus far. Any input or feedback would be greatly appreciated.

controller.js:

export default Ember.Controller.extend({
  //...
  objects: [],
  //...
  _pushToMatchings: function (response) {
    var tempArray = [];
    var pushed = false;
    for (var i = 0, length = this.get('objects.length'); i < length; i++) {
        pushed = false;
        var match = this.get('objects').objectAt(i);
        if (match.get('meta.items').objectAt(0) === response.get('meta.items').objectAt(0)) {
            tempArray.pushObject(response);
            pushed = true;
        } else {
            tempArray.pushObject(match);
        }
    }
    if (!pushed) {
        tempArray.pushObject(response);
    }
    this.set('objects', tempArray);
  },
  fetch: function() {
    var self = this;
    // find parent item
    this.get('store').find('item', id).then(function (item) {
        self._pushToMatchings(Ember.Object.create({
            meta: {
                items: [id],
                isLoading: true,
                label: item.get('label')
            },
            content: []
        }));
        self.set('isOpen', true);
        // child object
        self.get('store').find('child', searchParams).then(function (result) {
            (function (resultPtr) {
                Ember.run.next(function () {
                    setTimeout(function () { // @todo why do we need timeout here? whitout there is no fade out with opacity in css possible
                        resultPtr.set('meta.isLoaded', true);
                    }, 1); // 1 is enough but give spinner some time otherwise it looks ugly
                });
            }(result));
            result.set('meta.label', item.get('label'));
            self._pushToMatchings(result);
        }, function (error) { /* ... */ });
    }, function (error) { /* ... */ });

  }
}

controller.hbs:

<div>
    {{item-list close="close" elements=objects }}
</div>

item-list.js

export default Ember.Component.extend({
    elements: [],
    actions: {
        close: function () {
            this.sendAction('close');
        }
    }
});

item-list.hbs

<div class="items-list__buttons">
    <i class="icon-close_32" {{action "close" }}></i>
</div>
<div class="items-list__content">
    {{#each matching in elements}}
        <div class="items-list__item">
            <h2>{{t "items.offers" }} {{matching.meta.label}}</h2>
            {{spinner-element hideEvent=matching.meta.isLoaded }}
            <div {{bind-attr class=":items-list__box matching.meta.isLoaded:items--fadeIn" }}>
                {{#each item in matching.content}}
                    <div>
                        <!-- Render details of item -->
                    </div>
                {{/each}}
            </div>
        </div>
    {{/each}}
</div>

CSS:

.items-list__box {
    opacity: 0;
    transition: opacity 150ms ease 100ms;
}

.items--fadeIn {
    opacity: 1;
}

Answer №1

If you want to delay the execution of a function in Ember, you can use Ember.run.later which functions similar to setTimeout.

Ember.run.later(this ,function(){
     resultPtr.set('meta.isLoaded', true);
}, 100);

It may be necessary to add a slight delay to ensure that the transition occurs properly when rendering a div with the class "items--fadeIn". I tried this approach and it worked for me, so consider adjusting the time increment slightly if needed.

Answer №2

Although my response may be delayed, it is still relevant for those facing a similar dilemma:

The issue at hand involves Ember continuously re-rendering the entire list of items within your {{#each block. This occurs because each time there is a change, a completely new array of objects is passed in instead of modifying the existing objects' properties. To resolve this, you should establish an array of objects and update their properties individually to ensure that only the necessary objects are re-rendered.

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

Generate a see-through overlay when hovering over an image that matches the image's precise dimensions

I have encountered a challenge that I need help with. On a webpage, there are multiple images of varying sizes. I am looking to create a feature where hovering over an image triggers the appearance of a div above it containing a button and text without dis ...

What is preventing me from adjusting the background's position?

Recently, I've been working on a simple front page design but encountered an issue with moving my background. Despite trying different approaches like adjusting padding, the problem persists. As a beginner in coding, this has been quite a challenge, b ...

Div that scrolls only when children can be displayed without overflowing

I am seeking a solution to ensure that the children inside my scrollable div are only visible in their entirety. HTML <div id="container"> <div class="child"></div> <div class="child"></div> <div class= ...

What is the best way to turn an entire table into a clickable link that leads to a different HTML page?

I have created a table that I want to turn into a button linking to another page. The table's values need to be changeable based on data from a database, such as changing the parameter to pH and the value to 7. However, the button should redirect to t ...

Rearranging Information for Optimal Mobile Viewing

I am in the process of designing an HTML email template, and I am working on ensuring its compatibility with mobile devices. Currently, I have arranged an image, followed by text and a button, but my goal is to switch the order for mobile displays so that ...

Issue with Zurb Foundation 5.3 - Off Canvas: encountering a "TypeError: this[method] is undefined" error

I recently made some updates to a project using Foundation 5.3, but I'm struggling to get the Off Canvas feature to work properly. I've attempted different versions of jQuery (1.x - 2.x) without success. To troubleshoot, I created a simple jsfid ...

Application of stroke-linecap in SVG limited to single end only

Can a linecap be added to only one end of a stroke, rather than both ends like in the default sample below? <?xml version="1.0"?> <svg width="120" height="120" viewBox="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg"> < ...

Enhancing the menu bar and arranging elements using CSS and HTML coding

I am looking to redesign my homepage based on the layout of "kate spade" website that I found as an example. You can view the example website here. Specifically, I want to replicate the 4th example which is the "kate spade" website. It's important t ...

Problem encountered with submenu text in CSS drop-down menu

Hey there! I've recently started working on a website and encountered a problem while trying to create a dropdown menu using CSS and HTML. The hover function for the submenu is functioning properly, but I'm struggling with changing the font color ...

Challenges with styling <input> elements and autofill functionality

I am currently working on a login modal and have encountered an issue with styling input tags for browser autocomplete. Despite my efforts to reset the User Agent Stylesheet styling, the old styling reappears whenever autocomplete is triggered. Below is a ...

Creating a user-friendly navigation menu: A step-by-step guide

I need help creating a navigation menu for my website that isn't functioning properly. Here is the code I am currently using: body{ margin: 0px; padding: 0px; } nav > ul{ margin: 0px; padding: 0px; } nav > ul > li{ fl ...

Wrapping multiple floating divs with varying dimensions inside a parent div using shrinkwrap technique

To ensure that divs of varying numbers, widths, and heights have matching heights, I am implementing a jQuery script that requires them to float. However, I prefer for them to be centered, so I am utilizing the relative position trick for centering. CSS ...

Display content within DIV element without causing a page refresh

I'm experiencing a small issue with loading addresses in my divs. I have a link that triggers a dialog box and loads the linked address into it, but when I click on the link, the dialog box opens briefly and then everything disappears! Here's w ...

Having Trouble with Unevenly Spaced Child Elements in CSS Flexbox?

I'm running into a problem while utilizing CSS Flexbox to design a layout with multiple child elements. The issue at hand is that the child elements are not aligning correctly within the parent container. I aim for them to be evenly distributed and ce ...

The webpage is experiencing difficulty loading the image

Can someone help me troubleshoot an issue with loading an image in my HTML code? The image appears to have been sent but is not displayed. Could this be a CSS problem? (I am using materialized CSS) https://i.sstatic.net/sExue.png <html> {% extends ...

Issue with Bootstrap nav-bar not collapsing after hamburger icon has been used to expand it

Having a Bootstrap-related issue that I hope you can help me with. Whenever I expand my menu using the burger icon, it fails to collapse when I try to close it. Can you please point out my mistake and provide some guidance on how to prevent this from hap ...

Scrolling without limits - cylindrical webpage (CSS/JS)

Is it possible to create a page that infinitely scrolls from top to top without going straight back to the top, but instead showing the bottom content below after reaching it? Imagine being able to scroll up and see the bottom above the top like a 3D cylin ...

Compel the browser to initiate a reflow by adjusting the CSS

I am currently in the process of building a responsive image slider without relying on jQuery, using CSS3 transitions. The setup is quite simple: there's a viewport with a UL inside, containing relatively positioned LIs that are left floated. Howeve ...

The inner div appears to have content but is actually measured as 0x0 in size

Despite having content, my main div doesn't have a height or width. I am trying to make the main div fill 100% of the space between the header and footer so that the background image for main is displayed across the entire page excluding the header an ...

Invert the motion within a photo carousel

Is there anyone who can assist me with creating a unique photo slider using HTML, CSS, and JS? Currently, it includes various elements such as navigation arrows, dots, and an autoplay function. The timer is reset whenever the arrows or dots are clicked. Ev ...