From d5188777e46438afa57ba93f56633e3f2f6a1baf Mon Sep 17 00:00:00 2001 From: Ramon Wijnands Date: Sat, 29 Nov 2014 23:05:39 +0000 Subject: [PATCH] simplify searchBar with use of a callback function --- client/components/searchbar/searchbar.js | 46 +++++------------------- client/views/playlists/playlist.html | 2 +- client/views/playlists/playlist.js | 18 ++++++++++ 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/client/components/searchbar/searchbar.js b/client/components/searchbar/searchbar.js index a276c2c..4c7c611 100644 --- a/client/components/searchbar/searchbar.js +++ b/client/components/searchbar/searchbar.js @@ -1,38 +1,25 @@ -// initialize active_tab -Session.set('active_tab', 'songs'); - //Initialisation of searchbar component, // template example: -// {{> searchBar lovedToggle=0 viewToggle=0 collection="Playlists" playlistId=this._id }} +// {{> searchBar viewToggle=true callback="searchCallback" }} /*-------------------------- Available parameters - collection (Collection) Name of the collection to add objects to, requires extra code in - the add function, or sh*t will hit the fan (default: Playlists) - playlistId (int) pointer to which playlist songs should be added (required) + callback (string) The name of the helper of the parent template that + will be called when a song has been selected viewToggle (boolean) show/hide the toggle view buttons (default: show) lovedToggle (boolean) show/hide the add songs from loved songs button (default: show) ---------------------------*/ Template.searchBar.created = function( template ) { - //debug purposes - var d = Template.currentData() || {}; //at least return an obj - this.collection = (typeof d.playlistId !== "undefined") ? window[d.collection] : window.Playlists; - this.playlistId = (typeof d.playlistId !== "undefined") ? d.playlistId : null; + var d = this.data; this.lovedToggle = (typeof d.lovedToggle !== "undefined") ? d.lovedToggle : 1; this.viewToggle = (typeof d.viewToggle !== "undefined") ? d.viewToggle : 1; - this.lovedToggleWidth = (this.lovedToggle === 1) ? 1 : 0; this.viewToggleWidth = (this.viewToggle === 1) ? 2 : 0; this.searchBarWidth = 12 - this.lovedToggleWidth - this.viewToggleWidth; - console.log(this); - }; Template.searchBar.helpers({ - isActiveTab: function(route) { - return Session.equals("active_tab", route) ? "active" : ""; - }, lovedSongs: function() { var user = Meteor.user(); if (!user) @@ -47,11 +34,6 @@ Template.searchBar.helpers({ Template.searchBar.events = { - 'click ul.playlist-tabs > li': function (e) { - var li = $(e.currentTarget); - var route = li.data('id'); - Session.set("active_tab", route); - }, 'submit form.youtube-search': function (e) { e.preventDefault(); }, @@ -75,24 +57,14 @@ Template.searchBar.events = { //add search result to Meteor collection 'click .youtube-result, click a.loved': function(e, template) { + $("input.youtube-query").focus(); var videoId = (this.id !== undefined) ? this.id : this._id; - //see if search bar is linked up to a collection - if (!template.collection) { console.warn("no collection defined"); return; } - if (!template.playlistId) { console.warn("no collection id defined"); return; } - - console.log('queue video:', videoId, 'to playlist', template.playlistId); - $("input.youtube-query").focus(); - var songObject = { - "added" : new Date(), - "author" : Meteor.userId(), - "songId" : videoId - }; - template.collection.update( - { _id: template.playlistId}, - { $push: { songs: songObject} } - ); + // callback here + var callbackName = template.data.callback; + var cb = template.view.parentView.parentView.template.__helpers.get(callbackName); + cb(videoId); }, //Loved a song! diff --git a/client/views/playlists/playlist.html b/client/views/playlists/playlist.html index 10d0175..281f140 100644 --- a/client/views/playlists/playlist.html +++ b/client/views/playlists/playlist.html @@ -85,7 +85,7 @@

Stats

- {{> searchBar viewToggle=1 collection="Playlists" playlistId=this._id }} + {{> searchBar viewToggle=true callback="searchCallback" }} {{> songs}}
diff --git a/client/views/playlists/playlist.js b/client/views/playlists/playlist.js index 634ed40..80b6704 100644 --- a/client/views/playlists/playlist.js +++ b/client/views/playlists/playlist.js @@ -177,6 +177,24 @@ Template.playlist.events = { }, }; +Template.playlist.helpers({ + searchCallback: function (videoId) { + // this function will get called from the searchBar template + var playlistId = Template.parentData(1)._id; + + console.log('queue video:', videoId, 'to playlist', playlistId); + var songObject = { + "added" : new Date(), + "author" : Meteor.userId(), + "songId" : videoId + }; + Playlists.update( + { _id: playlistId}, + { $push: { songs: songObject} } + ); + } +}); + Template.updatePlaylistForm.editingDoc = function () { return Playlists.findOne({_id: this._id}); };