我把我的路由设置为
# 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"方法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对象确实已经返回。
任何想法?
在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);
}
})
更多信息请点击此链接