add series cleanup

This commit is contained in:
kay.one 2013-09-17 22:24:27 -07:00
parent 07b68b17ee
commit 927dbba945
2 changed files with 42 additions and 41 deletions

View File

@ -7,7 +7,8 @@ define(
'AddSeries/SearchResultCollectionView', 'AddSeries/SearchResultCollectionView',
'AddSeries/NotFoundView', 'AddSeries/NotFoundView',
'Shared/LoadingView', 'Shared/LoadingView',
], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView) { 'underscore'
], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView, _) {
return Marionette.Layout.extend({ return Marionette.Layout.extend({
template: 'AddSeries/AddSeriesTemplate', template: 'AddSeries/AddSeriesTemplate',
@ -25,18 +26,13 @@ define(
'click .x-load-more': '_onLoadMore' 'click .x-load-more': '_onLoadMore'
}, },
_onLoadMore: function () {
var showingAll = this.resultCollectionView.showMore();
if (showingAll) {
this.ui.loadMore.hide();
this.ui.searchBar.show();
}
},
initialize: function (options) { initialize: function (options) {
this.collection = new AddSeriesCollection({unmappedFolderModel: this.model});
this.isExisting = options.isExisting; this.isExisting = options.isExisting;
this.collection = new AddSeriesCollection();
if (this.isExisting) {
this.collection.unmappedFolderModel = this.model;
}
if (this.isExisting) { if (this.isExisting) {
this.className = 'existing-series'; this.className = 'existing-series';
@ -47,55 +43,65 @@ define(
} }
this.listenTo(this.collection, 'sync', this._showResults); this.listenTo(this.collection, 'sync', this._showResults);
this.resultCollectionView = new SearchResultCollectionView({
collection: this.collection,
isExisting: this.isExisting
});
this.throttledSearch = _.throttle(this.search, 1000, {trailing: true}).bind(this);
}, },
_onSeriesAdded: function (options) { _onSeriesAdded: function (options) {
if (options.series.get('path') === this.model.get('folder').path) { if (this.isExisting && options.series.get('path') === this.model.get('folder').path) {
this.close(); this.close();
} }
}, },
_onLoadMore: function () {
var showingAll = this.resultCollectionView.showMore();
if (showingAll) {
this.ui.loadMore.hide();
this.ui.searchBar.show();
}
},
onRender: function () { onRender: function () {
var self = this; var self = this;
this.$el.addClass(this.className); this.$el.addClass(this.className);
this.ui.seriesSearch.data('timeout', null).keyup(function () { this.ui.seriesSearch.keyup(function () {
window.clearTimeout(self.$el.data('timeout')); self.searchResult.close();
self.$el.data('timeout', window.setTimeout(function () { self._abortExistingSearch();
self.search.call(self, { self.throttledSearch({
term: self.ui.seriesSearch.val() term: self.ui.seriesSearch.val()
}); })
}, 500));
}); });
if (this.isExisting) { if (this.isExisting) {
this.ui.searchBar.hide(); this.ui.searchBar.hide();
} }
},
this.resultCollectionView = new SearchResultCollectionView({ onShow: function () {
collection: this.collection, this.searchResult.show(this.resultCollectionView);
isExisting: this.isExisting
});
}, },
search: function (options) { search: function (options) {
this.abortExistingSearch();
this.collection.reset(); this.collection.reset();
if (!options || options.term === '') { if (!options.term || options.term === this.collection.term) {
this.searchResult.close(); return;
} }
else {
this.searchResult.show(new LoadingView()); this.searchResult.show(new LoadingView());
this.collection.term = options.term; this.collection.term = options.term;
this.currentSearchPromise = this.collection.fetch({ this.currentSearchPromise = this.collection.fetch({
data: { term: options.term } data: { term: options.term }
}); });
}
return this.currentSearchPromise;
}, },
_showResults: function () { _showResults: function () {
@ -113,7 +119,7 @@ define(
} }
}, },
abortExistingSearch: function () { _abortExistingSearch: function () {
if (this.currentSearchPromise && this.currentSearchPromise.readyState > 0 && this.currentSearchPromise.readyState < 4) { if (this.currentSearchPromise && this.currentSearchPromise.readyState > 0 && this.currentSearchPromise.readyState < 4) {
console.log('aborting previous pending search request.'); console.log('aborting previous pending search request.');
this.currentSearchPromise.abort(); this.currentSearchPromise.abort();

View File

@ -8,11 +8,6 @@ define(
url : window.NzbDrone.ApiRoot + '/series/lookup', url : window.NzbDrone.ApiRoot + '/series/lookup',
model: SeriesModel, model: SeriesModel,
initialize: function (options) {
this.unmappedFolderModel = options.unmappedFolderModel;
},
parse: function (response) { parse: function (response) {
var self = this; var self = this;