如何从ember.js中的.json文件中检索模型 [英] How to retrieve models from .json file in ember.js

查看:140
本文介绍了如何从ember.js中的.json文件中检索模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的文件夹结构

Sample/
  css/
  js/
    libs/
    data/
        employees.json
    app.js
  index.html



app.js

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.json

{
  "employees": [
    {
        "name": "Title 1"
    },
    {
        "name": "Title 2"
    }
  ]
}

但是当我尝试获取数据来自.json文件,ember.js会自动用url替换.json中的。。

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

原始抓取电话:

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

但是,ember试图从

But ember trying to fetch data from

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

所以没有数据从适配器接收。 p>

So no data is recieved from the adapter.

推荐答案

@claptimes解决方案可能会工作,但它会复制原始的实现,我认为这是一个不好的做法,因为你必须手动保持最新版本的ember数据:)

@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:)

此外,我注意到您正在设置已被弃用一段时间的 url 属性( https://github.com/emberjs/data/blob/eeb6162f65513caa19ce18887c3f4d1c2014d80c/TRANSITION.md #host-and-namespace-configuration )。

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

这里有一个解决方案,调用 _super 和不覆盖完整的方法:

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

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

这篇关于如何从ember.js中的.json文件中检索模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆