基础上的从直观的像素转向我想出了以下解决方案。
我构建了以下网址: http://somedomain.com/#/searchresults/?lastname=king&firstname=stephen&city=somecity
如何网址是池莉构建在此不再赘述的方式。在我的情况下,我用表单和一些事件处理程序使用我自己的视图。
把我弄得工作看起来像这样的代码:
App.Router.map(function() {
this.resource("searchresults", { path: '/searchresults/:dynamic' });
});
App.SearchresultsRoute = Ember.Route.extend((function() {
var deserializeQueryString = function (queryString) {
if(queryString.charAt(0) === "?")
queryString = queryString.substr(1);
var vars = queryString.split('&'),
i = 0, length = vars.length,
outputObj = {};
for (; i < length; i++) {
var pair = vars[i].split('=');
outputObj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return outputObj;
};
return {
model: function(param) {
var paramObj = deserializeQueryString(param.dynamic);
return App.Searchresult.find(paramObj);
}
};
})()
);
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.create({
namespace: 'api'
})
});
App.Searchresult = DS.Model.extend({
lastname: DS.attr('string'),
firstname: DS.attr('string'),
street: DS.attr('string'),
hno: DS.attr('string'),
zip: DS.attr('string'),
city: DS.attr('string'),
country: DS.attr('string'),
birthdate: DS.attr('string')
});
这产生一个HTTP GET请求,我的REST API:
{"searchresults":
[{"id":"2367507","lastname":"King","firstname":"Stephen","street":"Mainstreet.","hno":"21" ...},
{"id":"3222409","lastname":"King","firstname":"Stephen","street":"Broadway","hno":"35" ...}
]}
:
http://somedomain.com/api/searchresults?lastname=king&firstname=stephen&city=somecity
我的REST API与响应
然后用这个模板将其显示出来:
<h2>Searchresults</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Street/Hno</th>
<th>City</th>
<th>Birthyear</th>
</tr>
</thead>
<tbody>
{{#each item in controller}}
<tr>
<td>{{item.firstname}} {{item.lastname}}</td>
<td>{{item.street}} {{item.hno}}</td>
<td>{{item.zip}} {{item.city}}</td>
<td>{{item.birthdate}}</td>
</tr>
{{/each}}
</tbody>
</table>
如果有人发现更优雅的方式,那不需要使用自定义的解串器,我会很乐意更新解决方案。 (另一个)丹尼尔提供的答案暗示http://somedomain.com/#/searchresults/king/stephen/somecity不适合我的情况,因为在我需要的解决方案中,我有超过10个不同的搜索标准/过滤器。用户通常只选择填写其中的一些。
这个例子上烬数据修订的基础:12和灰烬1.0.0-RC.3
这可能会实现。但是,我的实际模型和我的搜索表单比较复杂。用户可以选择10个以上的字段(名字,姓名,街道,住宅号码,出生日期,...)。为了进行搜索,用户通常仅为这些字段中的几个提供内容。如果我要这样构造URL,他们总是必须明确地包含所有的标准,即使这些字段中的大部分都是空的:即http:// localhost /#/ searchresults/king/stephen //////// somecity。我不喜欢,这个网址不能“显示”某个位置代表什么。即它不可见,那个位置3代表街道。 – Daniel 2013-05-12 11:37:28