【问题标题】:Ember use local JSON file instead of Mirage provided in tutorialEmber 使用本地 JSON 文件而不是教程中提供的 Mirage
【发布时间】:2018-05-26 09:06:03
【问题描述】:

我是构建网站的新手,在这个阶段我想做的就是使用本地 JSON 文件来检索数据,而不是 ember 教程中提供的 mirage。你有这样的 mirage/config.js:

export default function() {
  this.namespace = '/api';

  let rentals = [{
        //JSON
      }];


  this.get('/rentals', function(db, request) {
    if(request.queryParams.area !== undefined) {
      let filteredRentals = rentals.filter(function(i) {
        return i.attributes.area.toLowerCase().indexOf(request.queryParams.area.toLowerCase()) !== -1;
      });
      return { data: filteredRentals };
    } else {
      return { data: rentals };
    }
  });

  // Find and return the provided rental from our rental list above
  this.get('/rentals/:id', function (db, request) {
    return { data: rentals.find((rental) => request.params.id === rental.id) };
      });
}

This article 显示了部分解决方案,但我不知道它应该写在哪里。任何帮助将不胜感激。

【问题讨论】:

    标签: ember.js ember-data ember-cli ember-cli-mirage


    【解决方案1】:

    有几个不同的选项可以在不使用 mirage 的情况下对某些数据进行存根。最清晰和最简单的方法是 fetch。

    将你的 json 文件放在 public 文件夹中,我们称之为something.json。然后,使用 fetch 获取数据(这是路由的模型钩子):

    model() {
       return fetch('something.json')
        .then(function(res) {
            return res.json()
        })
    }
    

    此答案至少从 1.13 开始(可能更早)适用。它是从 3.1 开始编写的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 2015-12-09
      • 2021-09-11
      相关资源
      最近更新 更多