-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackbone-collection-search-ajax.js
executable file
·72 lines (56 loc) · 1.77 KB
/
backbone-collection-search-ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Backbone.Collection.search-ajax v0.2.1
// by Joe Vu - [email protected]
// For all details and documentation:
// https://github.com/homeslicesolutions/backbone-collection-search
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('underscore'), require('backbone'));
} else {
factory(root._, root.Backbone);
}
}(this, function (_, Backbone) {
// Extending out
_.extend(Backbone.Collection.prototype, {
//@ Search for AJAX version
search: function(keyword, attributes) {
// Closure
var that = this;
// Get new data
this.fetch({
parse: false,
data: {
keyword: keyword,
attributes: attributes
},
success: function( results ){
// Instantiate new Collection
var collection = new Backbone.Collection( results.models );
collection.searching = {
keyword: keyword,
attributes: attributes
};
collection.getSearchQuery = function() {
return this._searchQuery;
};
// Cache the recently searched metadata
that._searchResults = collection;
// Fire search event with new collection
that.trigger('search', collection );
}
});
},
//@ Get recent search query
getSearchQuery: function() {
return this.getSearchResults() && this.getSearchResults().getSearchQuery();
},
//@ Get recent search results
getSearchResults: function() {
return this._searchResults;
},
//_Cache
_searchResults: null
});
return Backbone;
}));