【问题标题】:Read JSON data from non-RESTful API with ajax in Ember在 Ember 中使用 ajax 从非 RESTful API 读取 JSON 数据
【发布时间】:2013-11-21 03:24:39
【问题描述】:

我正在为一个学习项目构建一个小型黑客新闻阅读器,我正在阅读的 API 有点不标准(所以除非有人知道如何硬塞它,否则 ember-data 是不可能的)。

项目列表在这里:http://node-hnapi.herokuapp.com/news 而单个项目是这样的:http://node-hnapi.herokuapp.com/item/6696691

这是我目前所做的:http://jsbin.com/OFeCoR/22/edit?html,js,output

App = Ember.Application.create()

baseURL = "http://node-hnapi.herokuapp.com"
newsURL = "/news"
itemURL = "/item/"

App.Items = Ember.Object.extend(  
  id: ""
  title: ""
  url: ""
  points: ""
  user: ""
  time_ago: ""
  comments_count: ""
  slug: (->
    @get("id")
  ).property("id")
)

App.Items.reopenClass 
  all: ->
    Ember.$.getJSON(baseURL + newsURL).then (response) ->
      items = []
      response.forEach (n) ->
        items.push App.Items.create(n)
      items     

App.Router.map ->
  @resource "items", ->
    @route "item",
      path: ":slug"

App.IndexRoute = Ember.Route.extend
  beforeModel: ->
    @transitionTo "items"

App.ItemsRoute = Ember.Route.extend
  model: ->
    App.Items.all()

App.ItemsItemRoute - Ember.Route.extend
  model: (params) ->
    itemID = App.Items.findProperty("id", params.id)
    Ember.$.getJSON((baseURL + itemURL + itemID).then (response) ->
      item = []
      response.forEach (i) ->
        item.push App.Item.create(i)
      item
    )

基本上,我试图从项目中的“项目”中获取 ID 以将其用于 slug 并在 ItemsItemRoute 中,将其推入 URL 以获取单个项目属性。我认为这是我出错的地方(ItemsItemRoute)。

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

【问题讨论】:

    标签: javascript ember.js coffeescript ember-data


    【解决方案1】:

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

    App.Router.map ->
      @resource "items"
      @resource "item",
        path: ":slug"
    

    但是如果你只是想获取已经获取的模型并保持相同的外观,那么没有理由重新获取数据,因为你已经获取了它,你应该只是使用 modelFor 并从父资源中获取它

    http://jsbin.com/eCOzOKe/1/edit

    App.ItemsItemRoute = Ember.Route.extend
      model: (params) ->
        itemsModel = @modelFor("items")
        item = itemsModel.findProperty("id", params.slug)
        item
    

    此外,您不需要使用 slug 这个词,您可以使用 :id 并从模型中删除计算属性,该属性仅返回 id 作为 slug

    【讨论】:

    • /item/ 资源(尤其是 cmets)中有几个不同的数据点。关于蛞蝓的好点。我最初的想法是把标题作为路线,但最终可能会很长。
    【解决方案2】:

    您的App.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 http://jsbin.com/OlUvON/1/edit

    【讨论】:

    • 啊,错别字很好……这非常简洁,谢谢。我想 params.slug 有效,因为 item 在 items 路由下?
    • 因为它在项目路径下。您不能在 items 路线中使用它。参数从 url 解析并传递到具有动态 slug 的路由中。只是为了让您知道,您不能在一条路线下有任何其他路线。可以在resources下添加资源,但是路由是死路一条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多