Revamping legacy code into an Ember component

Recently I started learning Ember and I'm really enjoying it! However, I'm facing some difficulties, especially when it comes to working with components.

Currently, I'm trying to convert some old code into a Component in Ember but I'm not sure where to start. I'm unsure how to capture the button click event and I've also noticed that Ember has many built-in helpers which might simplify the process.

Here is the result of the old code: http://codepen.io/anon/pen/WQjobV?editors=110

    // Old code snippets here

I have a series of boxes, where each box represents a booking for an event (this data will be fetched from the server). When a user clicks on a box, that box expands while the others disappear. Additionally, a dropdown menu should be created to allow users to navigate between events.

In my Ember project, I converted the "events" div into a component called "BookingBoxComponent" with two actions:

SiteApp.BookingBoxComponent = Ember.Component.extend({
  actions:
    open: function() {
      // HOW CAN I HANDLE THE CLICK EVENT HERE?
    },

    close: function() {
    }
});

As you can see, I have defined two actions for opening and closing the box. Should I simply include the logic in these actions, or is there a more efficient Ember-specific way?

If it's not too much to ask, I'd like to know how to access the clicked button in the open method. I tried passing it as a parameter like this:

<button {{action 'open' this}}></button>

But it didn't work as expected.

If someone can help me transform the old code into a more Ember-friendly format, I would be willing to offer 50 points as a token of appreciation.

Thank you.

Answer №1

Each action will come with an event object as the final parameter. By specifying this, you are actually passing the object with context in that specific block. When writing your open function, refrain from passing this and instead use

open: function(event) {
  // event.currentTarget will refer to the button
}

You can now easily access event.currentTarget or event.target

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

Tips for Utilizing CSS Overflow: Hidden to Prevent the Parent Container from Expanding

I have a fixed width column and a fluid column. In the fluid column, I want to display a string with nowrap so that it does not overflow the container. Here is an illustration of how I want the text to appear: --------------------------------------------- ...

I'm encountering a Type Error message in VS Code terminal stating that converting data to string is not a function. Can someone please help me understand this type error? I am new to Node.js and

const fileSystem = require('fs'); const data = fileSystem.readFileSync('example.txt'); if (data) console.log(data.toString()); console.log('Program Ended'); This is the code I wrote: and this is the error message my terminal ...

How can I show a tooltip when hovering over a tab using uib-tab?

Currently facing an issue with HTML/Bootstrap. My tabs are displayed using the uib-tabset directive on Angular Bootstrap (UI Bootstrap). I am trying to implement a tooltip that appears when hovering over a tab that is disabled. Does anyone know how I can a ...

Is there a way to find out the ultimate destination of a shortened URL without actually opening the webpage?

I am trying to implement a feature where I can click on links within email messages and have the full link displayed in a separate pane using jQuery. However, I am facing some challenges with this implementation. My plan is to use AJAX to call a PHP scrip ...

"Django's HttpResponse function removes double escapes from JSON data, causing issues with jQuery compatibility

I am encountering an intriguing issue with JSON and Django that I believe I've identified. The specific problem revolves around a JSON object structured like this: {"embed": "<iframe width='640' height='360' src='http://ww ...

Encountering issues with styled component's flex not functioning as expected in React

I am currently working with styled-components, and I have been using "flex" for responsive design. However, I am facing some challenges in implementing it. The CSS defined within styled-components does not seem to be applying properly. The styles are not ...

Issue with AjaxSetup overlapping with jqgrid

How can I set up global AJAX calls using ajaxsetup without interfering with the functionality of jqgrids on my website? I want to apply the code below to my AJAX calls, excluding those related to jqgrids. $.ajaxSetup({ type: "POST", contentType: ...

What is the best way to redirect a URL parameter that contains "?m=1"?

After transitioning from blogger to wordpress, I've noticed that many incoming links now have a "?m=1" parameter added to the end of my post link. For example: http://www.example.com/2015/06/name-of-blog-post.html?m=1 I've tried searching for ...

Is there a way to gather selected checkboxes from all child checkbox components in vue?

I have a unique setup where my table rows are generated by child components, each containing a checkbox. I am looking for a way to retrieve all the checked checkboxes at once without using two-way binding or updating an array on the parent component. Here ...

Utilizing spans to adjust the formatting of text within list items

When it comes to shaping text inside li elements, I've encountered a few issues. One of the problems is that the float-right divs aren't floating to the top right corner but instead leaving a space at the top. Additionally, the text isn't &a ...

Navigate to a different page using a sliding transition

Currently, I am working on a project using jquery-mobile where I need to redirect to another page after clicking on an image with a delay. This is what I have done so far: setTimeout(function(){ window.location.href = "myPage.html"; }, 1000); While t ...

What is the best way to completely eliminate a div from a webpage

I've created a new div element using jQuery: $(document.body).append('<div id="test"></div>'); Then, I display a success message and remove the div after 10 seconds: $('test').html('SUCCESS!'); setT ...

Strategies for managing rate-limited API services in React/Node.js: Handling ReferenceErrors

Hey there! I'm new to the world of React and NodeJS, and right now I'm navigating my way through dealing with a rate-limited API service. My plan is to have NodeJS fetch data from this API every half an hour, then send it to my "/youtube" endpoin ...

What could be the reason for one function returning undefined from identical API calls while the other functions produce successful results?

As I delved into incorporating the TMDB API into my project, a perplexing issue arose that has left me stumped. Despite utilizing identical code snippets in two separate files and functions, one of them returns undefined while the other functions flawlessl ...

Accessing the Parent Variable from a Function in JavaScript: A Guide

How can you properly retrieve the value of x? let x = 5 const f = (n:number) => { let x = "Welcome"; return x * n // Referring to the first x, not the second one } Also, what is the accurate technical term for this action? ...

Exploring Webpack: Combining CSS-modules with external CSS imports - A comprehensive guide!

In my React application using css-modules, I encountered an issue when trying to import CSS files from external libraries. Due to the inability to globally wrap them in a :global block, webpack interprets them as styles intended for objects only. Is there ...

Iterating through an object using the forEach method (uncommon practice)

Greetings, I have the following object: data = { name: undefined, age: undefined, gender: undefined }; I am looking to iterate through each key and value pair in this object and perform an action. Here is my attempt: this.data.forEach((item: ...

Using Knex in ExpressJS to insert a list of entries into SQLite with unique field constraints

I'm currently facing an issue with inserting a list of exercises into an sqlite database. The app is built on express JS and I am utilizing sqlite3 and knex to handle interactions with the DB. My goal is to add a set of exercises into the table exerci ...

Dealing with Array Splicing Issues in Angular

Being fairly new to the world of AngularJS, I suspect that I am just making a simple mistake. My goal is to splice the cardTypes array at the var newCard = cardTypes.shift(); line rather than using .shift() so that I can consider my ng-repeat index. Whil ...

Ways to avoid the cascading of CSS styles onto multiple Three.JS scenes

It seems like I might have to take the longer route to solve this issue, but let's give it a shot... I'm facing a challenge when applying CSS to control the opacity of a specific HTML element that acts as a container for a Three.JS scene. In thi ...