Ember - connecting to a JSON data source

Recently, I have been following a tutorial/example from codeschool and everything is going well. However, the example code includes the line:

App.ApplicationAdapter = DS.FixtureAdapter.extend();

Now, I want to maintain all the existing functionality but move the product data to an external JSON file.

Below is a snippet of my app.js file:

var App = Ember.Application.create({
LOG_TRANSITIONS: true
});

// Other code here...

App.ApplicationAdapter = DS.FixtureAdapter.extend();

// More code follows...

In addition, I have also included my HTML (index.html) file:

<!DOCTYPE html>
<html>
  <head>

    // Script and link references here...

</head>


<body>

   // Handlebars templates and scripts here...

</body>
</html>

The tutorial suggests creating a JSON file with specific content and changing the adapter in the app.js file to:

App.ApplicationAdapter = DS.RESTAdapter.extend();

I seem to be having trouble linking to the JSON file. Can anyone guide me on how to adjust the ApplicationAdapter to load the JSON file successfully?

UPDATE

To simplify this question:

  • I have index.html, app.js, and products.json files in the same directory

  • I need to update my app.js file as follows:

App.ApplicationAdapter = DS.RESTAdapter.extend({ xxxxxxxxx });

What should replace 'xxxxxx' in order to load my json file?

UPDATE

Update: I have managed to solve the issue! It turns out that I needed to include the path to my directory in the 'host' property:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: '/name of directory'
});

In my case where my project resides at localhost/ember, the following configuration works:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: '/ember'
});

Answer №1

I encountered a similar issue.

Instead of directly referencing the JSON file in your HTML, it is advisable to enhance the DS.RESTAdapter by incorporating a request for your file, as shown below:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: '/products.json?jsonp=?'
});

Give this approach a try. Feel free to reach out with any feedback!

Answer №2

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: '/custom directory name'
});

I recently had to make a change by removing the .json extension from my data file. Now it's simply named products (a plain text file). Whenever I try adding back the .json extension, the file cannot be located.

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

Dealing with JSON post requests in Node.js using Express 4

Attempting to develop a basic Express application that accepts JSON through a Post request. The current server-side code is as follows: var express = require('express'); var bodyParser = require('body-parser'); var app = express(); ap ...

Issue with Angular binding not updating after being assigned in promise.then() function

Within my angular application, I have a $scope variable labeled as user which contains a name property. Whenever I click on a link to set this user variable to "test": <a href="#" ng-click="setUser()">Set User</a> Using the following functio ...

Transforming button properties into a JSON format

I am currently in the process of developing a web application using node.js and mongodb. Within my app, there is a table that dynamically populates data from the database using a loop. I encountered an issue with a delete function that I implemented base ...

Choosing a personalized component using document selector

Currently, I am working on an application using Stenciljs and have created a custom element like this: <custom-alert alertType="warning" alertId="warningMessage" hide>Be warned</custom-alert> The challenge arises when attem ...

Invalid Boolean Parameter

Currently working on parsing a JSON string: Response: {"success":false,"displaymessage":"UserName or Email already exist. Please try again"} Here is the code snippet I am using: NSLog(@"Result: %@ %@", [responseObject valueForKeyPath:@"success"]? @"YE ...

Enhance your Next.js application with beautifully styled formatting using prettier

Looking for some assistance in setting up prettier with my next.js project. I have the following configuration defined in my package.json file: "scripts": { "format": "prettier --write \"**/*.{js, jsx}\"", }, My project structure includes sever ...

Guide on creating rsa key pair on the client side with angular2

Is there a way to generate an 'rsa' key-pair on the client-side using angular2? I am looking to create a private/public key pair and store the private key in a database while using the public key on the client side. How can this be achieved? I c ...

Struggling with transferring information from JavaScript to PHP through the use of Ajax

Currently, I am working on creating a string using a JavaScript function and then passing it to PHP. This is the block of code in my JavaScript file: <script> function passVal(){ var strUrl = buildStringUrl(lat1, lng1, lat2, ...

Tips for showcasing an array in nested elements within an Angular mat-tree

I'm new to Angular and I need help displaying an array's values within the child elements of a material tree. Specifically, I want to show the names of fruits (such as Apple, Banana...) in the child elements. The colors and discounts from the ar ...

Issue: ENOENT - The requested file or directory cannot be found in the context of an Angular2 and Express.js

I have made some changes to the Angular2 app on GitHub in order to use Express.js instead of KOA. However, when I try to load the app in FireFox, I encounter the following error in the `nodemon` console: Error: ENOENT: no such file or directory The Angul ...

Is it possible in Angular JS to only load a service in the specific JS file where it is needed, rather than in the app.js file

I attempted to do something like: var vehicle_info = angular.module('psngr.vehicle_info', []).factory('vehicle_info', ['$rootScope', '$timeout', '$q', vehicle_info]); var name = vehicle_info.getNameOfVclas ...

Verify the dates in various formats

I need to create a function that compares two different models. One model is from the initial state of a form, retrieved from a backend service as a date object. The other model is after conversion in the front end. function findDateDifferences(obj1, ...

Issues with Videojs Responsive Lightbox Functionality

I am looking for a solution to display VideoJS in a lightbox while keeping it responsive. I came across this helpful code snippet: https://github.com/rudkovskyi/videojs_popup. It seemed perfect until I tried using it with the latest version of Videojs and ...

Do not use Express.js to serve the index.html file

Encountered an issue when attempting to run my Express.js solution. Instead of opening my desired index.html file located in the client directory, it kept opening the index.jade file from the views folder with the message "Welcome to Express" on the browse ...

Why isn't this ajax script able to successfully transmit the data?

I'm having some trouble sending a JavaScript variable to a PHP file in order to create a query. Here's the script I've been using, but unfortunately it doesn't seem to be working. Any assistance would be greatly appreciated. <select ...

Quickly Share Documents within a Folder

I currently have a server set up with the following file structure: - index.js /client -index.html -about.html -signup.html -style.css Within the index.js file, I have implemented an express server. When the user navigates to /, it should display the ...

Unable to retrieve the status for the specified ID through the use of AJAX

When I click the button in my app, I want to see the order status. However, currently all statuses are being displayed in the first order row. PHP: <?php $query = mysqli_query($conn, "SELECT o.orderid,o.product,o.ddate,o.pdate,k.kalibariname,o.sta ...

"Utilizing PHP with FTP and AJAX, or other comparable technologies

After successfully creating a PHP class for FTP server interaction (connecting, changing directories, uploading files, etc.), I am now eager to provide real-time updates to users about the actions happening on the server. For example, notifying them with m ...

Learn how to successfully place a draggable object into a sortable container while ensuring that the dropped item is a personalized helper element rather than the original object

The jQuery draggable/sortable demo showcases the process of dropping a clone of the draggable item (both draggable and sortable elements share the same structure). However, I am interested in dropping a different DOM structure. For example, when dragging a ...

Should you hold off on moving forward until the asynchronous task finishes?

My goal is to retrieve location coordinates using the Google Maps JavaScript API in an asynchronous manner. Below is the function I've created for this purpose: function fetchCoordinates(address) { var geocoder = new google.maps.Geocoder(); ...