Fix only one movie showing. Fix more button not showing up.

pull/307/head
Leonardo Galli 8 years ago
parent c5ca2babf7
commit 4263808360

@ -9,178 +9,178 @@ var ErrorView = require('./ErrorView');
var LoadingView = require('../Shared/LoadingView'); var LoadingView = require('../Shared/LoadingView');
module.exports = Marionette.Layout.extend({ module.exports = Marionette.Layout.extend({
template : 'AddMovies/AddMoviesViewTemplate', template : 'AddMovies/AddMoviesViewTemplate',
regions : { regions : {
searchResult : '#search-result' searchResult : '#search-result'
}, },
ui : { ui : {
moviesSearch : '.x-movies-search', moviesSearch : '.x-movies-search',
searchBar : '.x-search-bar', searchBar : '.x-search-bar',
loadMore : '.x-load-more' loadMore : '.x-load-more'
}, },
events : { events : {
'click .x-load-more' : '_onLoadMore' 'click .x-load-more' : '_onLoadMore'
}, },
initialize : function(options) { initialize : function(options) {
console.log(options); console.log(options);
this.isExisting = options.isExisting; this.isExisting = options.isExisting;
this.collection = new AddMoviesCollection(); this.collection = new AddMoviesCollection();
if (this.isExisting) { if (this.isExisting) {
this.collection.unmappedFolderModel = this.model; this.collection.unmappedFolderModel = this.model;
} }
if (this.isExisting) { if (this.isExisting) {
this.className = 'existing-movies'; this.className = 'existing-movies';
} else { } else {
this.className = 'new-movies'; this.className = 'new-movies';
} }
this.listenTo(vent, vent.Events.MoviesAdded, this._onMoviesAdded); this.listenTo(vent, vent.Events.MoviesAdded, this._onMoviesAdded);
this.listenTo(this.collection, 'sync', this._showResults); this.listenTo(this.collection, 'sync', this._showResults);
this.resultCollectionView = new SearchResultCollectionView({ this.resultCollectionView = new SearchResultCollectionView({
collection : this.collection, collection : this.collection,
isExisting : this.isExisting isExisting : this.isExisting
}); });
this.throttledSearch = _.debounce(this.search, 1000, { trailing : true }).bind(this); this.throttledSearch = _.debounce(this.search, 1000, { trailing : true }).bind(this);
}, },
onRender : function() { onRender : function() {
var self = this; var self = this;
this.$el.addClass(this.className); this.$el.addClass(this.className);
this.ui.moviesSearch.keyup(function(e) { this.ui.moviesSearch.keyup(function(e) {
if (_.contains([ if (_.contains([
9, 9,
16, 16,
17, 17,
18, 18,
19, 19,
20, 20,
33, 33,
34, 34,
35, 35,
36, 36,
37, 37,
38, 38,
39, 39,
40, 40,
91, 91,
92, 92,
93 93
], e.keyCode)) { ], e.keyCode)) {
return; return;
} }
self._abortExistingSearch(); self._abortExistingSearch();
self.throttledSearch({ self.throttledSearch({
term : self.ui.moviesSearch.val() term : self.ui.moviesSearch.val()
}); });
}); });
this._clearResults(); this._clearResults();
if (this.isExisting) { if (this.isExisting) {
this.ui.searchBar.hide(); this.ui.searchBar.hide();
} }
}, },
onShow : function() { onShow : function() {
this.ui.moviesSearch.focus(); this.ui.moviesSearch.focus();
}, },
search : function(options) { search : function(options) {
var self = this; var self = this;
this.collection.reset(); this.collection.reset();
if (!options.term || options.term === this.collection.term) { if (!options.term || options.term === this.collection.term) {
return Marionette.$.Deferred().resolve(); return Marionette.$.Deferred().resolve();
} }
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 }
}); });
this.currentSearchPromise.fail(function() { this.currentSearchPromise.fail(function() {
self._showError(); self._showError();
}); });
return this.currentSearchPromise; return this.currentSearchPromise;
}, },
_onMoviesAdded : function(options) { _onMoviesAdded : function(options) {
if (this.isExisting && options.movie.get('path') === this.model.get('folder').path) { if (this.isExisting && options.movie.get('path') === this.model.get('folder').path) {
this.close(); this.close();
} }
else if (!this.isExisting) { else if (!this.isExisting) {
this.resultCollectionView.setExisting(options.movie.get('tmdbId')); this.resultCollectionView.setExisting(options.movie.get('tmdbId'));
/*this.collection.term = ''; /*this.collection.term = '';
this.collection.reset(); this.collection.reset();
this._clearResults(); this._clearResults();
this.ui.moviesSearch.val(''); this.ui.moviesSearch.val('');
this.ui.moviesSearch.focus();*/ //TODO: Maybe add option wheter to clear search result. this.ui.moviesSearch.focus();*/ //TODO: Maybe add option wheter to clear search result.
} }
}, },
_onLoadMore : function() { _onLoadMore : function() {
var showingAll = this.resultCollectionView.showMore(); var showingAll = this.resultCollectionView.showMore();
this.ui.searchBar.show(); this.ui.searchBar.show();
if (showingAll) { if (showingAll) {
this.ui.loadMore.hide(); this.ui.loadMore.hide();
} }
}, },
_clearResults : function() { _clearResults : function() {
if (!this.isExisting) { if (!this.isExisting) {
this.searchResult.show(new EmptyView()); this.searchResult.show(new EmptyView());
} else { } else {
this.searchResult.close(); this.searchResult.close();
} }
}, },
_showResults : function() { _showResults : function() {
if (!this.isClosed) { if (!this.isClosed) {
if (this.collection.length === 0) { if (this.collection.length === 0) {
this.ui.searchBar.show(); this.ui.searchBar.show();
this.searchResult.show(new NotFoundView({ term : this.collection.term })); this.searchResult.show(new NotFoundView({ term : this.collection.term }));
} else { } else {
this.searchResult.show(this.resultCollectionView); this.searchResult.show(this.resultCollectionView);
if (!this.showingAll && this.isExisting) { if (!this.showingAll) {
this.ui.loadMore.show(); this.ui.loadMore.show();
} }
} }
} }
}, },
_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();
} else { } else {
this._clearResults(); this._clearResults();
} }
}, },
_showError : function() { _showError : function() {
if (!this.isClosed) { if (!this.isClosed) {
this.ui.searchBar.show(); this.ui.searchBar.show();
this.searchResult.show(new ErrorView({ term : this.collection.term })); this.searchResult.show(new ErrorView({ term : this.collection.term }));
this.collection.term = ''; this.collection.term = '';
} }
} }
}); });

@ -4,59 +4,62 @@ var MoviesCollection = require('../Movies/MoviesCollection');
var vent = require('vent'); var vent = require('vent');
module.exports = Marionette.CollectionView.extend({ module.exports = Marionette.CollectionView.extend({
itemView : SearchResultView, itemView : SearchResultView,
initialize : function(options) { initialize : function(options) {
this.showExisting = true; this.showExisting = true;
this.isExisting = options.isExisting; this.isExisting = options.isExisting;
this.showing = 1; this.showing = 5;
vent.on(vent.Commands.ShowExistingCommand, this._onExistingToggle.bind(this)); if (this.isExisting) {
}, this.showing = 1;
}
_onExistingToggle : function(data) { vent.on(vent.Commands.ShowExistingCommand, this._onExistingToggle.bind(this));
this.showExisting = data.showExisting; },
this.render(); _onExistingToggle : function(data) {
}, this.showExisting = data.showExisting;
showAll : function() { this.render();
this.showingAll = true; },
this.render();
}, showAll : function() {
this.showingAll = true;
showMore : function() { this.render();
this.showing += 5; },
this.render();
showMore : function() {
return this.showing >= this.collection.length; this.showing += 5;
}, this.render();
setExisting : function(tmdbid) { return this.showing >= this.collection.length;
var movies = this.collection.where({ tmdbId : tmdbid }); },
console.warn(movies);
//debugger; setExisting : function(tmdbid) {
if (movies.length > 0) { var movies = this.collection.where({ tmdbId : tmdbid });
this.children.findByModel(movies[0])._configureTemplateHelpers(); console.warn(movies);
//this.children.findByModel(movies[0])._configureTemplateHelpers(); //debugger;
this.children.findByModel(movies[0]).render(); if (movies.length > 0) {
//this.templateHelpers.existing = existingMovies[0].toJSON(); this.children.findByModel(movies[0])._configureTemplateHelpers();
} //this.children.findByModel(movies[0])._configureTemplateHelpers();
}, this.children.findByModel(movies[0]).render();
//this.templateHelpers.existing = existingMovies[0].toJSON();
appendHtml : function(collectionView, itemView, index) { }
var tmdbId = itemView.model.get('tmdbId'); },
var existingMovies = MoviesCollection.where({ tmdbId: tmdbId });
if(existingMovies.length > 0) { appendHtml : function(collectionView, itemView, index) {
if(this.showExisting) { var tmdbId = itemView.model.get('tmdbId');
if (index < this.showing || index === 0) { var existingMovies = MoviesCollection.where({ tmdbId: tmdbId });
collectionView.$el.append(itemView.el); if(existingMovies.length > 0) {
} if(this.showExisting) {
} if (index < this.showing || index === 0) {
} else { collectionView.$el.append(itemView.el);
if (index < this.showing || index === 0) { }
collectionView.$el.append(itemView.el); }
} } else {
} if (index < this.showing || index === 0) {
collectionView.$el.append(itemView.el);
} }
}
}
}); });

Loading…
Cancel
Save