Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Ember中使用ajax从非RESTful API读取JSON数据_Javascript_Ember.js_Coffeescript_Ember Data - Fatal编程技术网

Javascript 在Ember中使用ajax从非RESTful API读取JSON数据

Javascript 在Ember中使用ajax从非RESTful API读取JSON数据,javascript,ember.js,coffeescript,ember-data,Javascript,Ember.js,Coffeescript,Ember Data,我正在为一个学习项目构建一个小的黑客新闻阅读器,我正在阅读的API有点不标准(因此,除非有人知道如何使用余烬数据,否则余烬数据是不可能的) 项目列表如下:而单个项目如下所示: 以下是我到目前为止所做的: 基本上,我试图从items中的一个“item”中获取ID,将其用于slug,并在ItemsItemRoute中,将其推到URL中以获取单个item属性。我想这就是我出错的地方(ItemsItemRoute) 我认为在单击链接/操作时,只获取单个项目数据可能是有意义的,而不是从一开始就获取所有项目

我正在为一个学习项目构建一个小的黑客新闻阅读器,我正在阅读的API有点不标准(因此,除非有人知道如何使用余烬数据,否则余烬数据是不可能的)

项目列表如下:而单个项目如下所示:

以下是我到目前为止所做的:

基本上,我试图从items中的一个“item”中获取ID,将其用于slug,并在ItemsItemRoute中,将其推到URL中以获取单个item属性。我想这就是我出错的地方(ItemsItemRoute)


我认为在单击链接/操作时,只获取单个项目数据可能是有意义的,而不是从一开始就获取所有项目数据。有什么想法吗?

如果你想拥有一个独立于父资源的资源,你应该把路由器改成这样:

App.Router.map ->
  @resource "items"
  @resource "item",
    path: ":slug"
但是,如果您只是想获取已经获取的模型并保持相同的外观,那么没有理由重新提取数据,因为您已经获取了数据,您应该只使用modelFor并从父资源获取数据


此外,您不需要使用slug这个词,只需使用
:id
并从模型中删除computed属性,该属性将
id
作为
slug

返回。ItemsItemRoute
有一些错误:

# you are using minus (-) this is a assigment and a equals (=) is needed
App.ItemsItemRoute - Ember.Route.extend
  model: (params) ->
    # App.Items.findProperty don't exist and params.id isn't present just params.slug because you mapped your route with path: ":slug"
    itemID = App.Items.findProperty("id", params.id)
    Ember.$.getJSON((baseURL + itemURL + itemID).then (response) ->          
      item = []
      # this will return just one item, no forEach needed
      response.forEach (i) ->
        item.push App.Item.create(i)
      item
    )
我更新了以下内容:

App.ItemsItemRoute = Ember.Route.extend
  model: (params) ->    
    Ember.$.getJSON(baseURL + itemURL + params.slug).then (response) ->
      App.Items.create(response)
并在每个项目中添加一个{{link to}},以便能够转换到
ItemsItemRoute


这是最新的jsbin

啊,很好地抓住了打字错误。。。这很简洁,谢谢。我想params.slug工作是因为item在items路由下?因为它在item路由下。您无法在项目路由中使用它。参数从url解析并传递到具有动态slug的路由中。只是想让你知道,你不能有任何其他路线下的路线。您可以在资源下添加资源,但路由是死路一条。在/item/resource中有几个不同的数据点(特别是注释)。这一点很好。我最初的想法是以标题作为路线,但最终可能会很长。
# you are using minus (-) this is a assigment and a equals (=) is needed
App.ItemsItemRoute - Ember.Route.extend
  model: (params) ->
    # App.Items.findProperty don't exist and params.id isn't present just params.slug because you mapped your route with path: ":slug"
    itemID = App.Items.findProperty("id", params.id)
    Ember.$.getJSON((baseURL + itemURL + itemID).then (response) ->          
      item = []
      # this will return just one item, no forEach needed
      response.forEach (i) ->
        item.push App.Item.create(i)
      item
    )
App.ItemsItemRoute = Ember.Route.extend
  model: (params) ->    
    Ember.$.getJSON(baseURL + itemURL + params.slug).then (response) ->
      App.Items.create(response)