The art of encapsulating JSON response objects in Ember Objects with a deep layer of wrapping

Currently, I am engaged in a project using Ember where I retrieve a complex JSON response in a Route's model function. In the associated template, I exhibit attributes of the response. Some of these attributes allow for specific actions that result in modifications to the model.

I aim to have these changes instantly reflected in the interface through Ember's binding feature. Regrettably, although accessing a top-level attribute from the JSON response in the ObjectController via this.get('attributeFromJson') is successful, attempting the same action on a child property does not yield the desired outcome.

To illustrate the issue, I have created the following JSFiddle: http://jsfiddle.net/KkD6U/

My understanding is that I must manually convert the response into an Ember.Object to leverage its Ember.Observable capabilities.

My question is: Is there a straightforward Ember method to automatically "deep-wrap" an entire JSON response into a structure comprised of Ember.Object instances for utilizing get and set across the entire response? How would one accomplish this in plain Ember?


Update:

Subsequently, I devised a basic function to transform regular JavaScript objects into Ember objects, view the jsFiddle here: http://jsfiddle.net/5vEcL/1/

Does this approach seem viable?

Answer №1

It seems to me that I'd have to manually convert the response into an Ember.Object in order to take advantage of its Ember.Observable mixin.

Yes, basically, if you wrap your child objects in Ember.Object.create(...), it will be compatible with ember's binding mechanism:

...
anArray: [
  Ember.Object.create({
    id: '1',
    anotherAttribute: '123'
  }),
  Ember.Object.create({
    id: '2',
    anotherAttribute: '456'
  })
]
...

You can view the modified jsfiddle example here: http://jsfiddle.net/ZZFkA/

I hope this explanation helps.

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

Is there a way to retrieve the initial item of a JSON array from an HTML document using Angular 2?

Within the src/assets/ directory, I have a json file called product.json with the following structure: [ { "images": "http://openclipart.org/image/300px/svg_to_png/26215/Anonymous_Leaf_Rake.png", "textBox": "empty", "comments": "empty" }, { "i ...

Accessing data from Execution Contexts in JavaScript

var value = 10; var outer_funct = function(){ var value = 20; var inner_funct = function(){ var value = 30; console.log(value); // displays 30 console.log(window["outer_funct"]["value"]); // I want to log the value 20 her ...

Mastering the usage of AngularJS Directive controllerAs syntax with scope is key to developing

I have included my code below: // HTML <body> <h1>{{foo.name}}</h1> <my-directive></my-directive> </body> // Scripts app.directive('myDirective', function() { return { restrict: 'E', ...

Struggling to comprehend the filtering arguments in VueJS?

While going through the VueJS Filter(orderby) API documentation, I came across some confusion regarding the arguments. Below is a sample for reference: Arguments: {String | Function} targetStringOrFunction "in" (optional delimiter) {String} [...s ...

Transforming an object containing methods into a string using Javascript specifically for Photoshop

I have a unique universal object that is essential for my Photoshop scripts. It contains various properties and methods like this: var Object = {}; Object.text = "this is some text"; Object.toolbox = new ToolBox(); // specialized object with its own funct ...

Find the correct file path for input using JavaScript or Angular 2

How can I retrieve the full file path of a selected Excel file using either pure JavaScript or Angular 2? I am looking to have the user select an Excel file, which will then be sent to a C# WEB API controller for further processing. Currently, my setup is ...

What is the solution for halting code execution in a foreach loop with nested callbacks?

Currently, I am in the process of setting up a nodejs database where I need to retrieve user information if the user exists. The issue I'm facing is that when I return callback(null) or callback(userdata), it does not stop the code execution and resul ...

Troubleshooting: Issues with Integrating Javascript and CSS into

I'm still new to this platform, so please correct me if I make any mistakes. My goal is to create a "task table" that allows users to edit existing tasks and add new ones. Thanks to the base template provided by Ash Blue, I've managed to program ...

Tips for displaying my libraries' function arguments while hovering in VSCode

As the creator of a JavaScript library, I am intrigued by how some libraries display function parameters and definitions when hovering over them in VSCode. Despite my efforts to add jsdoc style comments around the class definitions, I have not been able to ...

Is there a straightforward method to retrieve all information from Vue.js?

Is there a way to extract all of this data? I've attempted using this._data.forEach but it doesn't seem to be effective. Thank you! data() { return { childData: '', credit: '', company: '', email: ...

Tips for integrating CKEditor into an Angular 4 project

To start using CKEditor, I first installed it by running the command npm install ng2-ckeditor. Next, I included ckeditor.js in my index.html file. I then imported { CKEditorModule } from 'ng2-ckeditor'; in my app.module.ts file. After setup, I ...

Click the "Add to Cart" button to make a purchase

Recently, I've been working on modeling an add to cart feature using jQuery. However, I have encountered a small issue that I'm trying to troubleshoot. When I click the mybtn button, the model displays and functions correctly, but there seems to ...

Populating a textbox with a portion of my PHP array

I've got this JSON file with variable content. Here's an example link to it: The JSON data looks something like this: { "status": "ok", "total": 4, "results": { "distance": [...], "lng": [...], "lat": [...], "fourpp": [ ...

Is it possible to update both package-lock.json and package.lock simultaneously?

Before releasing to NPM, I always make sure to update the minor version. My usual process includes: - Modifying the package.json - Executing npm i to synchronize package-lock.json with the changes. This prepares both files for publishing. Is there a simpl ...

Having trouble interpreting the Json response from a jQuery Ajax POST request

When I receive a JSON from a REST API via Ajax, if I print the result to the console and then save it into a string variable (jsonInput) `JSON.parse` works without any issues. However, when I directly use the data from the Ajax call, it returns as undefi ...

Is there a way to transform a time string, for instance "8:02 AM", into a sortable field?

I am currently working with a MongoDB database that stores times as strings, and I want to filter queries based on specific time ranges. For instance, the time fields in my database are formatted as "8:02 AM" and "10:20 PM", and I am looking to refine my ...

What could be the reason for XMLHttpRequest's readystate being something other than 4?

Currently delving into the realm of AJAX for the first time, and endeavoring to construct a basic application that computes the required grade on a final exam based on past assessment results. The user keys in their grades into the form, subsequent to whi ...

Flask - Issue werkzeug.exceptions.BadRequestKeyError -

I've been encountering an issue while trying to run my first Flask app. After sending a POST request on Postman, I keep getting the following error: Error: werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The server couldn't understand t ...

Scrolling text box utilizing Jquery

Currently, I am utilizing a scrolling box that functions well * view here * under normal circumstances. However, when there is an extensive amount of content below it, such as: <article class="content"> ...

I'm curious if there is a method to incorporate localStorage into the initialState of Redux Toolkit within Next.js 14

Attempting to establish the initial value of a Redux Toolkit slice for dark mode using localStorage is proving problematic in Next.js, as the window object is not defined on the server-side, resulting in errors. The typical workaround involves using if (t ...