2013-03-29 22:17:03 +03:00
|
|
|
"use strict";
|
2013-06-21 04:43:58 +03:00
|
|
|
define(
|
|
|
|
[
|
|
|
|
'marionette',
|
|
|
|
'AddSeries/Collection',
|
|
|
|
'AddSeries/SearchResultCollectionView',
|
|
|
|
'Shared/SpinnerView',
|
|
|
|
'app',
|
|
|
|
'AddSeries/RootFolders/Collection',
|
|
|
|
'AddSeries/SearchResultView',
|
|
|
|
'Shared/SpinnerView'
|
|
|
|
], function (Marionette, AddSeriesCollection, SearchResultCollectionView, SpinnerView) {
|
|
|
|
return Marionette.Layout.extend({
|
|
|
|
template: 'AddSeries/AddSeriesTemplate',
|
|
|
|
|
|
|
|
ui: {
|
|
|
|
seriesSearch: '.x-series-search'
|
|
|
|
},
|
|
|
|
|
|
|
|
regions: {
|
|
|
|
searchResult: '#search-result'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.collection = new AddSeriesCollection();
|
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.ui.seriesSearch.data('timeout', null).keyup(function () {
|
2013-02-14 05:28:56 +03:00
|
|
|
window.clearTimeout(self.$el.data('timeout'));
|
|
|
|
self.$el.data('timeout', window.setTimeout(self.search, 500, self));
|
|
|
|
});
|
2013-01-23 08:48:22 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
this.resultView = new SearchResultCollectionView({ collection: this.collection });
|
|
|
|
},
|
2013-01-27 05:14:42 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
search: function (context) {
|
2013-01-27 05:14:42 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
context.abortExistingRequest();
|
2013-02-14 05:28:56 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
var term = context.ui.seriesSearch.val();
|
|
|
|
context.collection.reset();
|
2013-02-01 06:15:19 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
if (term === '') {
|
|
|
|
context.searchResult.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
context.searchResult.show(new SpinnerView());
|
2013-01-27 05:14:42 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
context.currentSearchRequest = context.collection.fetch({
|
|
|
|
data : { term: term },
|
|
|
|
success: function () {
|
|
|
|
context.searchResult.show(context.resultView);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2013-02-14 05:28:56 +03:00
|
|
|
|
2013-06-21 04:43:58 +03:00
|
|
|
abortExistingRequest: function () {
|
|
|
|
if (this.currentSearchRequest && this.currentSearchRequest.readyState > 0 && this.currentSearchRequest.readyState < 4) {
|
|
|
|
console.log('aborting previous pending search request.');
|
|
|
|
this.currentSearchRequest.abort();
|
|
|
|
}
|
2013-02-14 05:28:56 +03:00
|
|
|
}
|
2013-06-21 04:43:58 +03:00
|
|
|
});
|
2013-02-14 05:28:56 +03:00
|
|
|
});
|