简体   繁体   English

如何从Ember入门套件中显示简单的REST数据?

[英]How to show simple REST data in from Ember starter kit?

I downloaded the ember starter kit and want to show a simple REST API data to see how Ember works. 我下载了ember入门套件,并希望显示一个简单的REST API数据,以了解Ember的工作方式。

From videos and tutorials I found that there is a model hook that can be used to inject data. 从视频和教程中,我发现有一个model挂钩可用于注入数据。

So I did the following: 所以我做了以下事情:

App.Router = Ember.Router.extend({
  model: function ({
    return $.getJSON("https://api.foursquare.com/v2/users/self?oauth_token=TOKENHERE&v=20130723");
  }),
  root: Ember.Route.extend({
    index: Ember.Route.extend({
      route: '/'
    })
  })
})

And added the following in my index.html 并在我的index.html添加了以下内容

  <script type="text/x-handlebars" data-template-name="application">
      {{title}}
  </script>

However, this didn't work. 但是,这没有用。 When I go to index.html I don't see anything there. 当我转到index.html看不到任何内容。 Additionally, under Network tab of Inspect Element, I don't see any network requests being made to the REST API. 此外,在“检查元素”的“网络”选项卡下,我看不到对REST API发出任何网络请求。

What am I doing wrong? 我究竟做错了什么? Also, for sample purpose I would simply like to have a data.json json file that will contain some json data. 另外,出于示例目的,我只想拥有一个将包含一些json数据的data.json json文件。 However, I read that Ember lacks support to read a file, thats why I'm trying a sample rest api provided by foursquare. 但是,我看到Ember缺乏读取文件的支持,这就是为什么我要尝试由foursquare提供的示例rest api。

Is there a way to read a json file using ember? 有没有办法使用余烬读取json文件? I'm running this simply on my browser without a server. 我只在没有服务器的浏览器上运行它。

That looks like it should work. 看起来应该可以。 Here is an example using a different API but using the same structure: 这是一个使用其他API但使用相同结构的示例:

JavaScript: JavaScript的:

App = Ember.Application.create();

App.Router.map(function() {
  // put your routes here
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return $.getJSON("https://api.github.com/repos/emberjs/ember.js/stats/contributors");
  }
});

Templates: 范本:

  <script type="text/x-handlebars">
    <h2>Welcome to Ember.js</h2>

    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="index">
  Ember.js repository contributors
    <ul>
    {{#each user in controller}}
      <li>{{user.author.login}}</li>
    {{/each}}
  </ul>
  </script>

JSBin example JSBin示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM