最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve models from .json file in ember.js - Stack Overflow

programmeradmin0浏览0评论

Following is my folder structure

Sample/
  css/
  js/
    libs/
    data/
        employees.json
    app.js
  index.html
app.js
App = Ember.Application.create({
  LOG_TRANSITIONS: true
});

App.Router.map(function(){
  this.resource("employees");
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: "localapps/EmberProjects/Sample/js/data",
  url: "employees.json"
});

App.Employee = DS.Model.extend({
  name : DS.attr("string")
});

App.EmployeesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find("employee");
  }
});
employees.json
{
  "employees": [
    {
        "name": "Title 1"
    },
    {
        "name": "Title 2"
    }
  ]
}

But when i try to fetch data from the .json file, ember.js automatically replaces the .json in the url with " ".

Original fetch call:

http://localhost/localapps/EmberProjects/Sample/js/data/employees.json

But ember trying to fetch data from

http://localhost/localapps/EmberProjects/Sample/js/data/employees

So no data is recieved from the adapter.

Following is my folder structure

Sample/
  css/
  js/
    libs/
    data/
        employees.json
    app.js
  index.html
app.js
App = Ember.Application.create({
  LOG_TRANSITIONS: true
});

App.Router.map(function(){
  this.resource("employees");
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: "localapps/EmberProjects/Sample/js/data",
  url: "employees.json"
});

App.Employee = DS.Model.extend({
  name : DS.attr("string")
});

App.EmployeesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find("employee");
  }
});
employees.json
{
  "employees": [
    {
        "name": "Title 1"
    },
    {
        "name": "Title 2"
    }
  ]
}

But when i try to fetch data from the .json file, ember.js automatically replaces the .json in the url with " ".

Original fetch call:

http://localhost/localapps/EmberProjects/Sample/js/data/employees.json

But ember trying to fetch data from

http://localhost/localapps/EmberProjects/Sample/js/data/employees

So no data is recieved from the adapter.

Share Improve this question edited Nov 25, 2015 at 6:11 Giao1968 26k11 gold badges76 silver badges105 bronze badges asked Jan 2, 2014 at 13:18 DhakchianandanDhakchianandan 3511 gold badge6 silver badges15 bronze badges 4
  • Is the Employees file expected to change? Why not load it using the fixtures adapter for now? Are running your application as a file:// request in the browser or do you have a server? – claptimes Commented Jan 2, 2014 at 13:25
  • I am running my app from apache server. No the employees files is not going to change. I tried with Fixture adapter and it is working. In angular js, backbone js and other similar frameworks i will fetch json data from my local .json file file to test before connecting to production server. I am trying to do the same in ember.js also – Dhakchianandan Commented Jan 2, 2014 at 13:28
  • 1 Could you rename employees.json to employees? – claptimes Commented Jan 2, 2014 at 13:35
  • If i remove the .json extension from the employees, it is working. But is there is any solution available without replacing the .json extension – Dhakchianandan Commented Jan 2, 2014 at 13:38
Add a ment  | 

3 Answers 3

Reset to default 6

@claptimes solution would probably work, but it copies the original implementation which I think is a bad practice since you'll have to manually keep it up to date to the latest version of ember data:)

Also I noticed you're setting the url property which has been deprecated for a while (https://github./emberjs/data/blob/eeb6162f65513caa19ce18887c3f4d1c2014d80c/TRANSITION.md#host-and-namespace-configuration).

Here is a solution that calls _super and doesn't override the plete method:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  buildURL: function() {
    return this._super(...arguments) + '.json';
  }
});

To add the .json ending to your requests, you could override the buildURL call in the RESTAdapter. This should work:

DS.RESTAdapter = DS.Adapter.reopen({
  buildURL: function(type, id) {
    var url = [],
        host = get(this, 'host'),
        prefix = this.urlPrefix();

    if (type) { url.push(this.pathForType(type)); }
    if (id) { url.push(id); }

    if (prefix) { url.unshift(prefix); }

    url = url.join('/');
    if (!host && url) { url = '/' + url; }

    url += ".json";

    return url;
  }
});

I had the same problem, but all answers provided here didn’t work. I think that depends on the further development of Ember.js.

Actually I figured it out how to receive the data with the RESTAdapter. You have to add ?jsonp=? to the path to your JSON file.

Your adapter should look like this:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: "localapps/EmberProjects/Sample/js/data/employees.json?jsonp=?",
});
发布评论

评论列表(0)

  1. 暂无评论