Connect various models together, or create synchronized computed properties

At times, the model abstraction may fall short, leading to the necessity of synchronizing two different models.

For instance, I have two lists linked by an angular sortable, which requires a model structure like this:

left = [{name:"one"}, {name:"two"}];
right = [{name:"three"}];

However, I also want to provide the option for users to configure this using a list of checkboxes (implemented with the angular input.checkbox directive). This would result in a model like:

elements = [{
    name:"one",
    position:"left"
}, {
    name:"two",
    position:"left"
}, {
    name:"three",
    position:"right"
}]

In order to effectively connect the views to corresponding controllers, there needs to be some form of data view reflection from the same higher level model. Alternatively, there is a need for two models that can stay synchronized with each other.

This concept is reminiscent of having a computed property in the same model, but commonly the suggested solution involves using a function, which does not offer two-way binding on the data returned by the function.

Essentially, what is required here is functionality similar to that of filters - maintaining a transformed version of data in alignment with the original dataset.

How could one go about resolving such a dilemma?

Answer №1

To address my issue, I have come up with a solution that may not directly answer the question at hand, but I am including it here as a quick and temporary fix.

I have implemented a modified version of my model using a function wrapped in $scope.$watch, along with adding event handlers to ensure the model is updated correctly.

For a live demonstration, you can view a demo derived from the ui-sortable demo at http://codepen.io/danse/pen/xlptu

Alternatively, a more polished version created by a colleague can be found at http://codepen.io/danse/pen/ovGkq

While this solution works, I personally find it unattractive and believe it goes against the fundamental concept of Angular's bidirectional data binding.

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

Limit the number of API queries allowed in Node.js

In my quest to restrict queries made to the Battle.net API within their limits of 100 calls per second and 36,000 calls per hour, I am facing a challenge. The current implementation of my code is as follows: var async = require ('async'); var b ...

When a dropdown list only contains one item, the jQuery change event does not get triggered

I'm currently exploring ways to trigger events when options in a dropdown menu are clicked. I haven't come across any clear explanations on how to achieve this. What I specifically need is to activate the event and utilize the selected value. Usi ...

Conditionally assign a class to the body tag in your Jade template

Let's imagine I have a main template file called layout.jade and several files that extend this template - home, about, products, and more. Within the main template, I have structured it like this: body section.main-content b ...

Tips for triggering a method in the parent controller from a button inside a ui-grid enclosed within a custom directive

[summary] How can a button within a column template trigger a method in the surrounding controller when the grid is enclosed in a custom directive? [thanks] Let me illustrate using 3 plunkrs: P1) Implementing a basic action column in ui-grid http://pln ...

Is there a JavaScript function available that can be used to execute a script once content has been loaded via AJAX?

There are multiple inquiries regarding executing a JavaScript function after content is loaded via Ajax. I am familiar with the common solution of running JS after Ajax load completion. If more than 40 pages are loading a page my-Page.php using Ajax, is t ...

Is it possible to generate objects dynamically as the program is running?

Looking at the code snippet below, I currently have an array containing two JSON objects. However, if I need to create 20 objects, is there a more efficient way than manually writing each object in the array? Furthermore, is it possible to generate these o ...

Having difficulty capturing an error related to an invalid form body in discord.js

I built a command to send an embed based on JSON data, and it's working well for the most part. When I input the data correctly, the bot sends it to the channel as expected. However, if someone tries to insert text in a link section, it crashes the b ...

Storing text entered into a textarea using PHP

In a PHP page, I have a textarea and I want to save its content on click of a save button. The insert queries are in another PHP page. How can I save the content without refreshing the page? My initial thought was using Ajax, but I am unsure if it is saf ...

Karma jasmine and an angular controller that utilizes the 'Controller as' syntax (where 'this' is used instead of $scope)

I'm having trouble setting up karma for my unit tests, specifically on a basic example: Here is the controller file: angular.module('balrogApp.requests', [ /* Dependencies */ ]) // Routes configuration .config(['$routeProvider&a ...

Using Highcharts within a Vue.js component

I'm having trouble creating graphical components with Highcharts and Vue.js because I can't figure out how to dynamically set the id attribute that Highcharts needs to render properly. Does anyone know how to set the id dynamically? This is the ...

Is it possible to swap out the content within a <div> element with an external piece of HTML code using JQuery or JavaScript whenever the viewport dimensions are adjusted?

<html> <head> </head> function () { var viewportWidth = $(window).width(); if (viewportWidth < 700) { $('#wrapper').load('A.html'); }; <body> &l ...

How to boost the value of a property within a MongoDB document with Mongoose

Recently, I've been dealing with an array of objects named "items". This array includes different objects and I need to search for each object in MongoDB using the "baseId" found in the Base Document. Once I locate the matching object, I aim to update ...

Renew the Look of Dynamic HTML with Updated Styles

In the middle of my website, there is a console that contains links to dynamically update its content using javascript. Everything works smoothly, but the new HTML added dynamically does not have any styling applied to it. Is there a way to refresh the CS ...

invoking a function from two separate pages

I have created a React web app that is supposed to hide the navigation bar on the login page and then display it again after a successful login on all other pages. I achieved this by creating a function in App.js, but the issue arises when trying to call t ...

Troubleshooting issues with data parsing in an Angular typeahead module

Utilizing the AngularJS Bootstrap typeahead module, I am attempting to showcase data from an array of objects. Despite receiving data from my API call, I keep encountering the following error: TypeError: Cannot read property 'length' of undefine ...

React Native encountered an error: `undefined` is not an object

Encountering the commonly known error message "undefined is not an object" when attempting to call this.refs in the navigationOptions context. Let me clarify with some code: static navigationOptions = ({ navigation, screenProps }) => ({ heade ...

Create a function that multiplies every element in an array by 2

This example involves some JavaScript code. Currently, I have written the following: var double = function (array) { var newArray = []; for(var i = 0; i<array.length; i++) { newArray.push(array[i]); newArray.push(array[i]); ...

Remove the color options from the Material UI theme

Can certain color types be excluded from the MUI palette in MUI v5? For example, can background and error colors be removed, allowing only colors defined in a custom theme file to be used? I attempted using 'never' but it did not provide a solut ...

AngularJS form submission with and without page refresh

Looking to implement an AngularJS form directive where, if on /home page, redirect to /search/term and if already on /search page, submit without refreshing the page by simply changing the location. I am able to do both separately, but struggling to writ ...

In an AngularJS application, when using fullPage.js on the homepage, it is recommended to ensure that it is

Within my angularJs application, I have implemented fullPage.js on the homepage exclusively to ensure no interference with other pages or routes. To make this possible, I am currently triggering a destroy action during the locationChangeSuccess event. I a ...