Seeking ways to enable clicking on Components in EmberJS?

I've been experimenting with dynamic compartmentalization in EmberJS by passing a JSON structure into a Toolbar. Here is an example of what it looks like:

{
    draggable: true,
    buttons: [
      {label: "Portrait", action="vertical"},
      {label: "Landscape", action="horizontal"}
    ]
}

This Toolbar is used in a picture viewer to rotate photos vertically and horizontally. To make it reusable for other parts of the application, I turned it into a Component and linked the click event to the parent controller so that clicking on a button would rotate the picture. However, I'm having trouble adding the active class to the clicked button, most likely due to the nested nature of the buttons. How can I apply CSS styling to the clicked button by giving it the active class?

I attempted to set an `isActive` property in each button object when initializing and then setting it to true using the action function, but I couldn't figure out how to use observables with nested data structures. Do I need to turn each button into a separate component, or is there another solution?

Any help is appreciated.

Templates

<script type="text/x-handlebars" data-template-name="photo-gallery">
    <div id="PictureApp"></div>
    {{#if toolbar}}
        {{ui-toolbar options=toolbar buttonClicked="changeOrientation"}}
    {{/if}}
</script>

<script type="text/x-handlebars" data-template-name="components/ui-toolbar">
    {{title}}
    <div class="overlay">
        <div class="toolbar draggable">
            {{#if draggable}}
            <div class="header draggable-handle"></div>
            {{/if}}
            <ul>
                {{#each buttons}}
                    <li{{action "clicked" this}}>{{label}}</li>
                {{/each}}
            </ul>
        </div>
    </div>
</script>

JS

App.UiToolbarComponent = App.Component.extend({
    actions: {
        clicked: function(context){
            console.log(context);
            this.sendAction("buttonClicked", context.action);

        }
    },
    didInsertElement: function(){
        if(this.get("draggable")) {
            this.$(".draggable").draggable({handle: ".draggable-handle"});
        }
    }
});

Answer №1

It appears that you are heading in the right direction. Consider setting the active property on the button, but don't forget to clear the active flag on any other buttons as well.

Within the clicked action:

   clicked: function(context){
     console.log(context);
     this.buttons.setEach('active', false);
     context.set('active', true)
     this.sendAction("buttonClicked", context.action);
   }

Next, bind the class in your template:

            {{#each buttons}}
                <li {{bind-attr class=active}} {{action "clicked" this}}>{{label}}</li>
            {{/each}}

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 process for invoking an External Javascript Firestore function within a Typescript file?

Trying to figure out how to integrate a Firestore trigger written in an external JavaScript file (notifyNewMessage.js) into my TypeScript file (index.ts) using Node.js for Cloud functions. Both files are located in the same directory: https://i.stack.imgu ...

Click to start viewing the video

I'm attempting to have a video play when clicked, either on the video itself or around the video. There are multiple videos on the page and I've tried various solutions including the script below which didn't work. $('video'). ...

The function in JavaScript that is supposed to receive a value from a servlet is malfunctioning

I am encountering an issue with my JavaScript method that is supposed to display an alert on the document body's onload event. The method takes a single string parameter provided by the servlet. Although the value is retrieved successfully, it seems ...

Crafting a custom version of the $.data() function without relying on jQuery

, I am seeking guidance on how to eliminate jQuery from the code snippet below. It is my understanding that the $.data() method is employed to store data, but I am unsure of how to achieve this without using jQuery. document.querySelector('.sheet&apo ...

Using environmental variables in Nuxt 3 outside of component setup scripts can be easily achieved by accessing the variables directly

I have stored different API URLs in my .env file, specifically for development and production environments. Here is how I access these URLs: const isProdEnv = process.env.NODE_ENV === 'production' const DEV_API_URL = "https://example-stage.h ...

undefined is returned within a nested return statement

I need to pass some specific props, such as mapObject={this.props.mapObject} details={this.props.parsedData.categories[key], to another component called Item. However, I am encountering an issue where I receive a TypeError: this is undefined. Initially, t ...

What is the best way to display data from a list of objects in a table using React JavaScript?

I have been learning from tutorials on YouTube, where the instructor demonstrates displaying data successfully using hard-coded values. However, I want to retrieve data from the backend. Despite being able to fetch the data (confirmed by seeing it in the c ...

Creating a customized design for a React application with the use of Scss and JavaScript variables

Is there a method to incorporate JavaScript variables into an SCSS file? For instance, I have a JavaScript file containing the following code: // sky blue const PRIMARY_COLOR = '#489FEF' export { PRIMARY_COLOR} and I would like to utilize it ...

Press the Enter key to submit

Encountering issues while trying to enter an event. Despite reviewing several posts on this matter, a solution has not been found yet. The project needs to function properly in Chrome, FF & IE (8,9,10,11), but it is currently not working on any browser. T ...

A guide on extracting/filtering information from JSON files using JavaScript

Is it possible to extract specific data from a JSON file using JavaScript? I have imported a JSON file into my local JavaScript and I am having difficulty in retrieving and displaying certain information. Could someone offer assistance with this? The JS ...

Struggling to fix the "TypeError: not a function" issue

Hey everyone, I've been working on setting up an Express app to connect to mongodb using the node driver for mongodb. Unfortunately, I keep encountering a TypeError: mongodbconnect is not a function. I double-checked my export/import process and made ...

Running a Node.js server on a public IP address while utilizing socket.io

Take a look at the server code snippet below: express = require('express'); app = express(); app.use('/', express.static(__dirname + '/')); http = require('http').Server(app); io = require('socket.io')(htt ...

What is the best way to make the Three.js camera focus on the front side of an object?

I have a unique sphere composed of hexagons and pentagons, and my goal is to align the camera directly with a specific hexagon so that it appears flat in the center of the user's view. The hexagons are created using the hexasphere.js plugin (https:// ...

JavaScript setInterval causing exception due to syntax error

I am trying to play an audio file every second using the code below. The audio file is one second long and consists of a ticking sound. However, when I run the code, it only plays once before throwing an error in the console. My goal is to have the audio p ...

In what way does AngularJS asynchronously navigate through the Angular template made up of HTML and AngularJS Directives?

While diving into the AngularJS book penned by Brad Green and Shyama Sheshadari, I stumbled upon the following insightful passage: The initial startup sequence unfolds as follows: 1. A user triggers a request for your application's first page. ...

Possible revision: "Methods for updating Bootstrap language settings?"

Currently, I am working on a project using ASP.NET Core in combination with Bootstrap. Recently, I successfully integrated localization support by following the guidance provided in this documentation. However, I encountered an issue where the Bootstrap fr ...

Can News be generated without the need to add a new _id in Mongoose?

Whenever I attempt to make revisions to my document, a problem arises. Upon creating a new object, an error is thrown indicating that my current field already contains an _id field. let freshArticle = new News({ title: "title" ...

Is it possible to selectively export certain interfaces within a .d.ts file?

// configuration.d.ts export interface Configuration { MENU_STRUCTURE: Node[]; } interface Node { name: string; } Looking at the snippet above, I am aiming to only export Configuration. However, I noticed that I can also import Node from an ext ...

Encountering an issue in Android Studio 1.3.1 when trying to create a SQLite

I encountered an error on line 26 of MainActivity.java. A Java NULLPOINTER Exception is being displayed. Can someone please advise me on how to resolve this issue? My code is available on GitHub. https://github.com/happyshravan/SQLite Latest LogCat error: ...

Click to remove with jQuery

My code currently wraps the label text in a span tag, but I want to remove the span tag when each span is clicked. For example, if I have test1 and test2 added, under Refined by:, clicking on test1 should remove that specific label or clicking on test2 sh ...