Sju*_*sen 5 ember.js ember-data
我在使用NDB的Google App Engine项目中使用EmberJs和Ember-Data.在数据库中,我有Host,Probe和Check实体.只要我的REST api按顺序排列,数据库模型并不重要,但为了清楚起见,这里是我的数据库类:
class Host(ndb.Model):
hostName = ndb.StringProperty()
hostKey = ndb.Key('Host', 'SomeHostId')
class Probe(ndb.Model):
checkName = ndb.StringProperty()
probeKey = ndb.Key('Host', 'SomeHostId', 'Probe', 'SomeProbeId')
class Check(ndb.Model):
checkName = ndb.StringProperty()
checkKey = ndb.Key('Host', 'SomeHostId', 'Probe', 'SomeProbeId', 'Check', 'SomeCheckId')
Run Code Online (Sandbox Code Playgroud)
我添加了密钥以显示每个主机上都运行了一些探针,每个探针都执行一些检查.
在我的App.Js中,我定义了以下模型:
App.Host = DS.Model.extend({
hostName: DS.attr('string')
probes: DS.hasMany('probe',{async:true})
});
App.Probe = DS.Model.extend({
host: DS.belongsTo('host'),
probeName: DS.attr('string')
checks: DS.hasMany('check',{async:true})
});
App.Check = DS.Model.extend({
probe: DS.belongsTo('probe'),
hostName: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
我已经定义了以下路由器:
App.Router.map(function() {
this.resource('hosts', function(){
this.resource('host', { path:':host_id'}, function(){
this.resource('probes', function(){
this.resource('probe', { path:':probe_id'}, function(){
this.resource('checks', function(){
this.resource('check', { path:':check_id'}, function(){
});
});
});
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
在AppEngine中,如果已构建以下URL路径:
app = webapp2.WSGIApplication([
('/', MainHandler),
webapp2.Route('/hosts', HostsHandler),
webapp2.Route('/hosts/<hostId>/', HostHandler),
webapp2.Route('/hosts/<hostId>/probes', ProbesHandler),
webapp2.Route('/hosts/<hostId>/probes/<probeId>/checks', ChecksHandler),
webapp2.Route('/hosts/<hostId>/probes/<probeId>/checks/<checkId>/', CheckHandler)
])
Run Code Online (Sandbox Code Playgroud)
{
"hosts": [
{
"hostName": "SomeHostName1",
"id": "SomeHostId1"
},
{
"hostName": "SomeHostName2",
"id": "SomeHostId2"
}
]
}
Run Code Online (Sandbox Code Playgroud)
http://example.com/hosts/SomeHostId1/probes返回:
{
"probes": [
{
"probeName": "SomeProbeName1",
"id": "SomeProbeId1",
"host_id": "SomeHostId1"
},
{
"probeName": "SomeProbeName2",
"id": "SomeProbeId2",
"host_id": "SomeHostId1"
}
]
}
Run Code Online (Sandbox Code Playgroud)
http://example.com/hosts/SomeHostId1/probes/SomeProbeId1/checks返回:
{
"checks": [
{
"checkName": "SomeCheckName1",
"id": "SomeCheckId1",
"probe_id": "SomeProbeId1"
},
{
"checkName": "SomeCheckName2",
"id": "SomeCheckId2",
"probe_id": "SomeProbeId1"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的模板是:
<script type="text/x-handlebars" id="host">
<h3>{{hostName}}</h3>
{{#link-to 'probes' probes}}probes{{/link-to}}
{{outlet}}
</script>
<script type="text/x-handlebars" id="probes">
{{#each probe in probes}}
Probe: {{probe.probeName}}
{{#link-to 'checks' probe.checks}}checks{{/link-to}}
{{/each}}
{{outlet}}
</script>
<script type="text/x-handlebars" id="checks">
{{#each check in checks}}
Check: {{check.checkName}}
{{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)
现在我已经拥有了所有这些...但不知道如何将它们组合在一起,以便Ember-Data提供正确的http请求.到目前为止我只看到请求访问http://example.com/modelName/
| 归档时间: |
|
| 查看次数: |
4153 次 |
| 最近记录: |