コレクションをフィルタリングする方法のように、モデルをフィルタリングすることが可能かどうか疑問に思っていましたか?
私はスポーツ サイトの検索機能を実行しています。検索結果をサッカー、テニス、バスケットボール、水泳、スポーツなどの種類でフィルタリングできるようにしたいと考えています。
これが私のコードです(filterSearch()
メソッドを確認してください):
define([
'jquery',
'backbone',
'underscore',
'models/model'],
function($, Backbone, _, Model){
var Search = Model.extend({
urlRoot: '/search',
defaults: {
query: ''
},
initialize: function(attrs, options) {
if (typeof options === 'object') {
this.router = options.router;
}
},
filterSearch: function(type) {
this.filter(function(data) {
return data.get(type);
});
}
});
return Search;
});
JSON:
[
{
"search": [
{
"result": {
"query": "Vettel is world champion"
},
"type": "Formula 1",
"id": 1
},
{
"result": {
"query": "Romario of Brazil world cup 1994"
},
"type": "football",
"id": 2
},
{
"result": {
"query": "federe won again"
},
"type": "tennis",
"id": 3
}
]
}
]