Please hold off on confirming your RSVP until further interactions have taken place

I have a feature where a component triggers an action when swiped, sending it upwards to the parent route/controller for handling some ajax functionality. The component includes UI elements that indicate a loading state while the action is being processed. Once the action is complete and working gets set back to false, the loading animation stops and user interaction can resume.

callAjaxAction() {
  this.setProperties({working:true});
  Ember.RSVP.cast(this.attrs.action()).finally(() => {
    this.$('.draggable').animate({
      left: 0
    });
    this.setProperties({working:false});
  });
}

In this scenario, the controller captures the action specified in the component definition and executes an ajax function to retrieve data for display on the page.

// in the controller action
return new Ember.RSVP.Promise((resolve,reject) => {
    Ember.$.ajax({
      type: 'get',
      dataType: 'json',
      url: `http://***/api/paysources/user/697?key=${ENV.APP.api_key}`
    }).then((response)=>{
      this.setProperties({
        'cards':response.user_paysources,
        'showCards': true
      });
    },(reason)=>{
      reject();
      this.get('devlog').whisper(reason);
    })
  })

This process involves displaying a popup component allowing the user to select a payment card. Upon clicking away or completing the payment with a selected card, the UI needs to be reset not only on the current page but also send a signal to the swipe component indicating that the loading process is finished.

In my concept, I envision a way to trigger an action on a component from the parent controller/route. Is there a method to achieve this?

Answer №1

When it comes to triggering an action on a component from the parent controller or route, unfortunately there is no direct way to do so in Ember. However, there are two common approaches that can be used:

  1. One workaround could be moving the Ajax request into the component itself.
  2. Adopting the 'data down, actions up' pattern, which involves firing an action on the component that updates a property on the controller.

In this scenario, the original component's template and controller would need to be structured as follows:

original-component.js

reset: computed('transactionComplete', function() {
  // perform cleanup tasks here...
})

original-component-template.hbs

{{#if transactionComplete}}
  {{! display content upon completion of transaction... }}
{{/if}}

controller.js

transactionComplete: false,

actions: {
  completeTransaction() {
    this.toggleProperty('transactionComplete');
  }
}

controller-template.hbs

{{original-component
  transactionComplete=transactionComplete
}}

{{cart-component
  transactionComplete=(action 'completeTransaction')
}}

cart-component.js

actions: {
  processTransaction() {
    // perform cleanup tasks here...
    this.attrs.transactionComplete();
  }
}

There may be alternative and more efficient methods available based on your specific requirements for resetting the original component. Additionally, have you explored the possibility of utilizing Ember Data and routing for loading the cards?

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

Transform the given string into an array by eliminating all instances of the plus symbol

I need help converting strings into an array by removing the "+" characters. Currently, it is only converting the first element and not all of them. Can you assist me in converting all instances of "+" to "," for every element? let myString = 'jan + f ...

Ways to retrieve the text linked to a checkbox

Is there a way to access the text associated with a checkbox using its attribute? For example, in the code below, I want to alert the user with "Selected -- TextXYZ" when the checkbox is clicked. <form id="idForm" class="classForm"> <input type ...

Protractor tests are successful when run locally, but encounter failures when executed on Travis-CI

I recently integrated end-to-end tests using Protractor into my AngularJS application. Testing them locally showed that they all pass, but upon committing to GitHub and Travis for CI, most of the tests fail. The failing tests seem to be those requiring na ...

The ajaxForm function is failing to execute properly and is not providing any error messages

When trying to use jQuery ajaxForm, I encounter a strange issue. I am attempting to set up a form for file upload with progress percentage tracking. However, my ajaxForm function does not seem to be triggering at all. Below is the code snippet I am usin ...

Using AngularJS's ng-repeat directive to convert expressions into JavaScript code

I am successfully retrieving data from my Database using AngularJS, however, I am struggling to find a way to extract the data from ng-repeat and store it in a JavaScript Object. Is the data treated as an expression after ng-repeat is applied? I have hear ...

Steps to show a jquery modal popup when a button is clicked in mvc 4

In my MVC project, I have a button that should trigger a popup with a textbox and another button when clicked. However, my current implementation is causing the textbox and button to be displayed on the page initially instead of appearing in the popup as i ...

What is the best way to create a large-scale matrix using SVG technology?

I want to create a 10000X10000 matrix in canvas/svg, with each cell containing a square. I attempted to accomplish this using canvas and two loops: for(i=0; i<10000; i++){ for(j=0;j<10000; j++){ /*some drawing code*/ console.log(i,' ...

Matching request parameters with a JSON object using node.js and express framework

After uncommenting the lines, the code runs smoothly. However, whenever I enable the 'else' statement, I consistently receive a 'not found' message even when there is a match between req.params.code and data.airports[i].code. var exp ...

Can you explain how to extract information from an API response using res.send?

Utilizing the MEAN stack in JavaScript for my single page application has been seamless. A crucial component of my architecture involves an Angular factory that communicates with my API. app.factory('authorizing', function($resource){ retur ...

Facing issues connecting to my MongoDB database as I keep encountering the error message "Server Selection Timed Out After 3000ms" on MongoDB Compass

I am encountering an error on my terminal that says: { message: 'connect ECONNREFUSED 127.0.0.1:27017', name: 'MongooseServerSelectionError', reason: TopologyDescription { type: 'Single', setName: null, maxS ...

The inner nested ng-repeat section is not properly binding to the scope variable and appears to be commented out

In my code, there is a nested ng-repeat set up. The 'boards' variable in the scope is an array that contains another array called 'tasks', which also consists of arrays. In the innermost ng-repeat, I am attempting to bind to task.conten ...

Formik's handleChange function is causing an error stating "TypeError: null is not an object (evaluating '_a.type')" specifically when used in conjunction with the onChange event in DateInput

When using the handleChange function from Formik with the DateInput component in "semantic-ui-calendar-react", I encountered an error upon selecting a date. https://i.stack.imgur.com/l56hP.jpg shows the console output related to the error. AddWishlistFor ...

Offsetting touch events in an iPhone's UIWebView(Note: This

Exploring the canvas drawing feature in a UIWebView, I am developing an app that enables finger painting on the iPhone. Currently facing a challenge where I need to offset the touch so that users can see what they are painting without their finger obstru ...

Running a script upon service initialization

Can code be run when a service is first initialized? For instance, if the product Service is being initialized, I'd like to execute the following code: this.var = this.sharedService.aVar; ...

Executing Cascading Style Sheets (CSS) within JQuery/Javascript

I've been encountering a problem with my website. I have implemented grayscale filters on four different images using CSS by creating an .svg file. Now, I'm looking to disable the grayscale filter and show the original colors whenever a user clic ...

Vanilla.js with Vue does not support the onclick event functionality

Currently, I am facing the need to utilize vanilla.js in my application due to the presence of html entities retrieved from the database that require special treatment. Since the babel compilation process has already concluded, I am resorting to manipula ...

Transforming RGB Decimal Color to HEX RGB Color in Three.js

Currently, I am facing a challenge in my task involving colors while working with three.js, a JavaScript library. In this particular task, I need to convert decimal color codes (e.g. 12615680) into formats like #FF0000 or 0xFF0000. I am seeking a JavaScr ...

Incorporate Vue into a template using variables and props

Currently in the process of building a vue app and wondering how I can seamlessly integrate it into a template while passing variables (props) into it. When I execute npm run dev, the app and all its components are coded with no issues. After running npm ...

When I try running my JavaScript code in the console, it works perfectly fine. However, I encounter an error when I attempt

Recently, I added a cool JavaScript code to my website that changes the background of the landing page randomly. Here is the snippet: var bgImages = [ "url('assets/bg/front1.jpg')", "url('assets/bg/fro ...

Animate a nested element dynamically with jQuery

Whenever I click on the "view" button, I am dynamically adding data to a div. This feature is working perfectly fine. However, I wanted to improve it by adding another nested dynamic element. The problem I'm facing now is that when I try to expand al ...