Revert "updated backgrid" Also fixed his naive capitalization...

This reverts commit 8fd7def9dd.
This commit is contained in:
Mark McDowall 2013-06-27 00:23:00 -07:00
parent dca7274f35
commit b81b39550f
4 changed files with 267 additions and 543 deletions

View file

@ -6,12 +6,10 @@
Licensed under the MIT @license.
*/
(function (root) {
(function ($, _, Backbone, Backgrid, lunr) {
"use strict";
var Backbone = root.Backbone, Backgrid = root.Backgrid, lunr = root.lunr;
/**
ServerSideFilter is a search form widget that submits a query to the server
for filtering the current collection.
@ -38,17 +36,14 @@
/** @property {string} [name='q'] Query key */
name: "q",
/**
@property {string} [placeholder] The HTML5 placeholder to appear beneath
the search box.
*/
/** @property The HTML5 placeholder to appear beneath the search box. */
placeholder: null,
/**
@param {Object} options
@param {Backbone.Collection} options.collection
@param {string} [options.name]
@param {string} [options.placeholder]
@param {String} [options.name]
@param {String} [options.placeholder]
*/
initialize: function (options) {
Backgrid.requireOptions(options, ["collection"]);
@ -56,21 +51,16 @@
this.name = options.name || this.name;
this.placeholder = options.placeholder || this.placeholder;
// Persist the query on pagination
var collection = this.collection, self = this;
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection &&
collection.mode == "server") {
collection.queryParams[this.name] = function () {
return self.searchBox().val() || null;
return self.$el.find("input[type=text]").val();
};
}
},
searchBox: function () {
return this.$el.find("input[type=text]");
},
/**
Upon search form submission, this event handler constructs a query
parameter object and pass it to Collection#fetch for server-side
@ -78,22 +68,9 @@
*/
search: function (e) {
if (e) e.preventDefault();
var data = {};
// go back to the first page on search
var collection = this.collection;
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection &&
collection.mode == "server") {
collection.state.currentPage = 1;
}
else {
var query = this.searchBox().val();
if (query) data[this.name] = query;
}
collection.fetch({data: data, reset: true});
data[this.name] = this.$el.find("input[type=text]").val();
this.collection.fetch({data: data});
},
/**
@ -102,8 +79,8 @@
*/
clear: function (e) {
if (e) e.preventDefault();
this.searchBox().val(null);
this.collection.fetch({reset: true});
this.$("input[type=text]").val(null);
this.collection.fetch();
},
/**
@ -138,7 +115,8 @@
e.preventDefault();
this.clear();
},
"keydown input[type=text]": "search",
"change input[type=text]": "search",
"keyup input[type=text]": "search",
"submit": function (e) {
e.preventDefault();
this.search();
@ -146,14 +124,14 @@
},
/**
@property {?Array.<string>} [fields] A list of model field names to
search for matches. If null, all of the fields will be searched.
@property {?Array.<string>} A list of model field names to search
for matches. If null, all of the fields will be searched.
*/
fields: null,
/**
@property [wait=149] The time in milliseconds to wait since for since the
last change to the search box's value before searching. This value can be
@property wait The time in milliseconds to wait since for since the last
change to the search box's value before searching. This value can be
adjusted depending on how often the search box is used and how large the
search index is.
*/
@ -165,9 +143,9 @@
@param {Object} options
@param {Backbone.Collection} options.collection
@param {string} [options.placeholder]
@param {string} [options.fields]
@param {string} [options.wait=149]
@param {String} [options.placeholder]
@param {String} [options.fields]
@param {String} [options.wait=149]
*/
initialize: function (options) {
ServerSideFilter.prototype.initialize.apply(this, arguments);
@ -177,8 +155,11 @@
this._debounceMethods(["search", "clear"]);
var collection = this.collection = this.collection.fullCollection || this.collection;
var collection = this.collection;
var shadowCollection = this.shadowCollection = collection.clone();
shadowCollection.url = collection.url;
shadowCollection.sync = collection.sync;
shadowCollection.parse = collection.parse;
this.listenTo(collection, "add", function (model, collection, options) {
shadowCollection.add(model, options);
@ -186,15 +167,9 @@
this.listenTo(collection, "remove", function (model, collection, options) {
shadowCollection.remove(model, options);
});
this.listenTo(collection, "sort", function (col) {
if (!this.searchBox().val()) shadowCollection.reset(col.models);
});
this.listenTo(collection, "reset", function (col, options) {
this.listenTo(collection, "sort reset", function (collection, options) {
options = _.extend({reindex: true}, options || {});
if (options.reindex && col === collection &&
options.from == null && options.to == null) {
shadowCollection.reset(col.models);
}
if (options.reindex) shadowCollection.reset(collection.models);
});
},
@ -243,9 +218,7 @@
when all the matches have been found.
*/
search: function () {
var matcher = _.bind(this.makeMatcher(this.searchBox().val()), this);
var col = this.collection;
if (col.pageableCollection) col.pageableCollection.getFirstPage({silent: true});
var matcher = _.bind(this.makeMatcher(this.$("input[type=text]").val()), this);
this.collection.reset(this.shadowCollection.filter(matcher), {reindex: false});
},
@ -253,7 +226,7 @@
Clears the search box and reset the collection to its original.
*/
clear: function () {
this.searchBox().val(null);
this.$("input[type=text]").val(null);
this.collection.reset(this.shadowCollection.models, {reindex: false});
}
@ -289,7 +262,7 @@
@param {Object} options
@param {Backbone.Collection} options.collection
@param {string} [options.placeholder]
@param {String} [options.placeholder]
@param {string} [options.ref] lunrjs` document reference attribute name.
@param {Object} [options.fields] A hash of `lunrjs` index field names and
boost value.
@ -300,7 +273,7 @@
this.ref = options.ref || this.ref;
var collection = this.collection = this.collection.fullCollection || this.collection;
var collection = this.collection;
this.listenTo(collection, "add", this.addToIndex);
this.listenTo(collection, "remove", this.removeFromIndex);
this.listenTo(collection, "reset", this.resetIndex);
@ -378,17 +351,15 @@
query answer.
*/
search: function () {
var searchResults = this.index.search(this.searchBox().val());
var searchResults = this.index.search(this.$("input[type=text]").val());
var models = [];
for (var i = 0; i < searchResults.length; i++) {
var result = searchResults[i];
models.push(this.shadowCollection.get(result.ref));
}
var col = this.collection;
if (col.pageableCollection) col.pageableCollection.getFirstPage({silent: true});
col.reset(models, {reindex: false});
this.collection.reset(models, {reindex: false});
}
});
}(this));
}(jQuery, _, Backbone, Backgrid, lunr));