Javascript js RESTful模型关系
关于从restful服务器(由Sails.js使用MongoDB提供服务)检索数据,我在Ember.js中遇到了一个与模型相关的问题 我的路由器设置如下:Javascript js RESTful模型关系,javascript,rest,ember.js,ember-data,sails.js,Javascript,Rest,Ember.js,Ember Data,Sails.js,关于从restful服务器(由Sails.js使用MongoDB提供服务)检索数据,我在Ember.js中遇到了一个与模型相关的问题 我的路由器设置如下: App.Router.map(function() { this.route("dashboard"); this.resource('exams', {path: "/exams"}, function() { this.resource('exam', {path: ":exam_id"}, functio
App.Router.map(function() {
this.route("dashboard");
this.resource('exams', {path: "/exams"}, function() {
this.resource('exam', {path: ":exam_id"}, function(){
this.resource('questions', function() {
this.route("new");
})
})
this.route("view", {path: "/:exam_id" }); // for viewing exam details
});
});
基本上,我得到了一个可用考试的列表,可以编辑每个考试的一些细节,也可以点击它查看相关的问题列表
我对使用restful适配器访问的考试资源没有任何问题,如下所示:
http://localhost:1337/api/v1/exams
这将产生:
{
exams: [
{
user_id: "52444c03268a31ea0b000001",
title: "Introduction test",
private: false,
active: false,
showResults: false,
random: false,
_id: "52471342445565e74600000a"
},
...
]
}
问题资源未嵌入,存储在MongoDB中,是一个单独的集合,可以单独访问:
http://localhost:1337/api/v1/questions
结果:
{
questions: [
{
_id: "52483f404617e6c728c4ed93",
title: "What's the capital of Moscow?",
exam_id: "52471342445565e74600000a"
},
{
_id: "52483f6e4617e6c728c4ed94",
title: "What's the capital of Switzerland?",
exam_id: "52471342445565e74600120a"
}
]
}
但是,问题应该始终与考试相关。据我所知,您还不能在Ember.js中嵌套rest路由。我理想的休息路线是:
http://localhost:1337/api/v1/exams/52471342445565e74600000a/questions
为了得到一个特定考试的所有问题,但我不确定这是否可以做到。至少我从来没有想过让它发挥作用
因此,为了保持简单,我决定使用exam_id restfully查询问题,以仅获取与特定考试相关的问题列表:
http://localhost:1337/api/v1/questions/52471342445565e74600000a //<-- exam_id
http://localhost:1337/api/v1/questions/52471342445565e74600000a // 如果你使用余烬数据,大部分都很简单
查询字符串部分很简单:
this.get('store').find('question',{test_id:123213})代码>
会产生一个类似于“/api/questions?test\u id=123213”的GET
关系部分非常棒,也非常简单:
App.BlogPost = DS.Model.extend({
comments: DS.hasMany("comment")
});
App.Comment = DS.Model.extend({
post: DS.belongsTo("blogPost")
});
你在使用余烬数据吗?