0

我的路线设置为

# ROUTER MAPPING
App.Router.map( ->
  this.resource("alertInstances", 
    path:"/"
  )

  this.resource("alertInstance", 
    path:"/:alertInstance_id"
  )
)

# ALERT INSTANCES ROUTE
App.AlertInstancesRoute = Ember.Route.extend(
  model: ->
    App.AlertInstance.all()

  setupController: (controller, model) ->
    controller.set("content", model)
)

# ALERT INSTANCE ROUTE
App.AlertInstanceRoute = Ember.Route.extend(
  model: (params) ->
    App.AlertInstance.find(params.alertInstance_id)

  setupController: (controller, model) ->
    controller.set("content", model)
)

当我转到路线“/”时,我会得到所有“警报实例”的列表,然后单击一个,它会显示单个“警报实例”的详细信息。

我的模型对象设置如下。

# ALERT INSTANCE MODEL
App.AlertInstance = Ember.Object.extend()
App.AlertInstance.reopenClass(
  allAlertInstances: []
  currAlertInstance:null
  all: ->
    @allAlertInstances = []
    $.ajax
      url:base + "api/alert_instances"
      dataType: "JSON"
      context:this 
      success: (response) ->
    for i in response
      i.clinical = if i.alert_type is "clinical" then true else false
      @allAlertInstances.addObject App.AlertInstance.create(i)
    @allAlertInstances
)

这一切都很好。但是当用户直接转到“/10”时(通过指定警报实例ID),由于没有从服务器检索到任何数据,因此没有任何内容可显示。

所以我添加了以下“查找”方法

find: (id) ->
  @currAlertInstance = null
  $.ajax 
    url:base + "api/alert_instance"
    dataType:"JSON"
    context:this
    data:
      id:id
      success: (response) ->
        @currAlertInstance = App.AlertInstance.create(response)
        @currAlertInstance
  @currAlertInstance

但是视图是空的,什么也没有显示。

我可以在我的 Chrome 控制台网络中看到确实返回了一个警报实例 JSON 对象。

有任何想法吗?

4

1 回答 1

0

在 Route 中使用 render 来渲染模板。

App.AlertInstancesRoute = Ember.Route.extend({
  render: function() {
    this.render([<handlebar_name>], {
       [outlet: <outlet_name>],
       [into: <template_name_to_render_to>]
    });
  },

  model: function(params) {
    App.AlertInstance.all();
  },

  setupController: function(controller, model) {
    controller.set("content", model);
  }
})

欲了解更多信息,请点击此链接

于 2013-03-05T09:35:10.497 回答