规范变更 - 如何重构我的 JavaScript 逻辑?
这可能非常简单,但目前我想不出一个好方法来做到这一点(有一个完整的大脑放屁)。
我目前有一个脚本,可以从 YouTube 频道获取视频,并显示当前视频和下面的 3 个缩略图,并使用左右箭头导航到前一个或后三个缩略图以选择要观看的另一个视频。
规范刚刚(再次)更改,现在单击箭头时只需向左或向右移动 1 个而不是 3 个。
即:
当前逻辑:
<< [1] [2] [3] >> **click right** << [4] [5] [6] >>
所需逻辑
<< [1] [2] [3] >> **click right** << [2] [3] [4] >>
代码:
var youtube =
{
author : "XXXXXXX",
pageNr : 0,
pageSize : 3,
pageCount : 1,
videos : [],
gets : function()
{
var _this = this;
var url = "http://gdata.youtube.com/feeds/api/videos?author=" + this.author + "&start-index=" + (this.pageNr * this.pageSize + 1) + "&max-results=" + this.pageSize + "&v=2&orderby=published&alt=json-in-script&fields=openSearch:totalResults,entry(id,title,media:group(media:thumbnail,yt:videoid),yt:statistics)";
$.getJSON(url + "&callback=?",
function(response)
{
_this.show(response.feed.entry);
});
},
start : function()
{
var _this = this;
var url = "http://gdata.youtube.com/feeds/api/videos?author=" + this.author + "&start-index=" + (this.pageNr * this.pageSize + 1) + "&max-results=" + this.pageSize + "&v=2&orderby=published&alt=json-in-script&fields=openSearch:totalResults,entry(id,title,media:group(media:thumbnail,yt:videoid),yt:statistics)";
$.getJSON(url + "&callback=?",
function(response)
{
_this.pageCount = Math.floor(response.feed.openSearch$totalResults.$t / 3);
_this.show(response.feed.entry);
$(".video-thumb").eq(0).click();
});
},
next : function()
{
if(this.pageNr >= this.pageCount){ return; }
this.pageNr++;
$(".video-thumb-picture-wrapper,#slider TD").removeClass("selected");
this.gets();
},
prev : function ()
{
if(this.pageNr <= 0){ return; }
this.pageNr--;
$(".video-thumb-picture-wrapper,#slider TD").removeClass("selected");
this.gets();
},
show : function(videos)
{
var _this = this;
_this.videos = [];
$(videos).each(function(index,video)
{
_this.videos.push(
{
id : video.media$group.yt$videoid.$t,
title : video.title.$t,
viewCount : (video.yt$statistics && video.yt$statistics.viewCount ? video.yt$statistics.viewCount : 0),
thumbnails : video.media$group.media$thumbnail
});
});
$(".video-thumb-picture").each(function(index,div)
{
var video = index < _this.videos.length ? _this.videos[index] : { id : "", title : "", viewCount : 0, thumbnails : [{ url : "" }] };
$(div)
.css("background-image","url('" + video.thumbnails[0].url + "')")
.parent().next().html(video.title)
.parent().css("visibility",(index < _this.videos.length) ? "visible" : "hidden");
});
}
};
var appId = '';
window.fbAsyncInit = function()
{
FB.init(
{
appId : appId,
status : true,
cookie : true,
xfbml : true
});
FB.Canvas.setAutoResize();
};
$(function()
{
$(".arrow-left").click(function()
{
youtube.prev();
});
$(".arrow-right").click(function()
{
youtube.next();
});
youtube.start();
$("#div-share").unbind('click').click(function()
{
FB.ui(
{
method : 'feed',
name : '',
link : ''
});
});
$(".video-thumb").click(function()
{
var div = $(this);
if(div.css("visibility") == "hidden"){ return; }
$(".video-thumb-picture-wrapper,#slider TD").removeClass("selected");
div.children().first().addClass("selected").parent().parent().addClass("selected");
var videoNr = parseInt(div.attr("videonr"));
var video = youtube.videos[videoNr];
var html = '<object id="div-video" style="height: 270px; width: 425px" width="425">';
html += '<param name="play" value="true">';
html += '<param name="wmode" value="transparent">';
html += '<param name="movie" value="http://www.youtube.com/v/' + video.id + '?autohide=1&autoplay=1&fs=1&feature=player_embedded" />';
html += '<param name="allowFullScreen" value="true" />';
html += '<param name="allowScriptAccess" value="always" />';
html += '<embed src="http://www.youtube.com/v/' + video.id + '?autohide=1&autoplay=0&fs=1&feature=player_embedded" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="425" height="270" wmode="transparent"></embed>';
html += '</object>';
$("#div-video").html(html);
$("#div-video-title").html(video.title);
$("#b-video-views").html(video.viewCount);
$("#div-like").html('<fb:like href="http://www.youtube.com/watch?v=' + video.id + '" show_faces="false" width="350"></fb:like>');
FB.XFBML.parse(document.getElementById('div-like'));
$("#div-share").unbind('click').click(function()
{
FB.ui(
{
method : 'feed',
name : video.title,
link : ''
});
});
});
});
This is probably quite straight-forward but at the moment I can't think of a good way to do this (having a complete brain fart).
I currently have a script that fetches videos from a youtube channel and displays the current video and 3 thumbnails underneath with a left and right arrow to navigate to the previous or next three thumbs to choose another video to watch.
The spec just changed (again) and now it needs to move just one to the left or right when an arrow is clicked instead of 3.
i.e. :
Current Logic:
<< [1] [2] [3] >> **click right** << [4] [5] [6] >>
Desired Logic
<< [1] [2] [3] >> **click right** << [2] [3] [4] >>
Code:
var youtube =
{
author : "XXXXXXX",
pageNr : 0,
pageSize : 3,
pageCount : 1,
videos : [],
gets : function()
{
var _this = this;
var url = "http://gdata.youtube.com/feeds/api/videos?author=" + this.author + "&start-index=" + (this.pageNr * this.pageSize + 1) + "&max-results=" + this.pageSize + "&v=2&orderby=published&alt=json-in-script&fields=openSearch:totalResults,entry(id,title,media:group(media:thumbnail,yt:videoid),yt:statistics)";
$.getJSON(url + "&callback=?",
function(response)
{
_this.show(response.feed.entry);
});
},
start : function()
{
var _this = this;
var url = "http://gdata.youtube.com/feeds/api/videos?author=" + this.author + "&start-index=" + (this.pageNr * this.pageSize + 1) + "&max-results=" + this.pageSize + "&v=2&orderby=published&alt=json-in-script&fields=openSearch:totalResults,entry(id,title,media:group(media:thumbnail,yt:videoid),yt:statistics)";
$.getJSON(url + "&callback=?",
function(response)
{
_this.pageCount = Math.floor(response.feed.openSearch$totalResults.$t / 3);
_this.show(response.feed.entry);
$(".video-thumb").eq(0).click();
});
},
next : function()
{
if(this.pageNr >= this.pageCount){ return; }
this.pageNr++;
$(".video-thumb-picture-wrapper,#slider TD").removeClass("selected");
this.gets();
},
prev : function ()
{
if(this.pageNr <= 0){ return; }
this.pageNr--;
$(".video-thumb-picture-wrapper,#slider TD").removeClass("selected");
this.gets();
},
show : function(videos)
{
var _this = this;
_this.videos = [];
$(videos).each(function(index,video)
{
_this.videos.push(
{
id : video.media$group.yt$videoid.$t,
title : video.title.$t,
viewCount : (video.yt$statistics && video.yt$statistics.viewCount ? video.yt$statistics.viewCount : 0),
thumbnails : video.media$group.media$thumbnail
});
});
$(".video-thumb-picture").each(function(index,div)
{
var video = index < _this.videos.length ? _this.videos[index] : { id : "", title : "", viewCount : 0, thumbnails : [{ url : "" }] };
$(div)
.css("background-image","url('" + video.thumbnails[0].url + "')")
.parent().next().html(video.title)
.parent().css("visibility",(index < _this.videos.length) ? "visible" : "hidden");
});
}
};
var appId = '';
window.fbAsyncInit = function()
{
FB.init(
{
appId : appId,
status : true,
cookie : true,
xfbml : true
});
FB.Canvas.setAutoResize();
};
$(function()
{
$(".arrow-left").click(function()
{
youtube.prev();
});
$(".arrow-right").click(function()
{
youtube.next();
});
youtube.start();
$("#div-share").unbind('click').click(function()
{
FB.ui(
{
method : 'feed',
name : '',
link : ''
});
});
$(".video-thumb").click(function()
{
var div = $(this);
if(div.css("visibility") == "hidden"){ return; }
$(".video-thumb-picture-wrapper,#slider TD").removeClass("selected");
div.children().first().addClass("selected").parent().parent().addClass("selected");
var videoNr = parseInt(div.attr("videonr"));
var video = youtube.videos[videoNr];
var html = '<object id="div-video" style="height: 270px; width: 425px" width="425">';
html += '<param name="play" value="true">';
html += '<param name="wmode" value="transparent">';
html += '<param name="movie" value="http://www.youtube.com/v/' + video.id + '?autohide=1&autoplay=1&fs=1&feature=player_embedded" />';
html += '<param name="allowFullScreen" value="true" />';
html += '<param name="allowScriptAccess" value="always" />';
html += '<embed src="http://www.youtube.com/v/' + video.id + '?autohide=1&autoplay=0&fs=1&feature=player_embedded" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="425" height="270" wmode="transparent"></embed>';
html += '</object>';
$("#div-video").html(html);
$("#div-video-title").html(video.title);
$("#b-video-views").html(video.viewCount);
$("#div-like").html('<fb:like href="http://www.youtube.com/watch?v=' + video.id + '" show_faces="false" width="350"></fb:like>');
FB.XFBML.parse(document.getElementById('div-like'));
$("#div-share").unbind('click').click(function()
{
FB.ui(
{
method : 'feed',
name : video.title,
link : ''
});
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 gets() 和 start():
在 start():
就像 aepheus 提到的那样,此时我可能会将 pageNr 重命名为 curIndex (或类似的名称)。
In gets() and start():
In start():
Like aepheus mentions, I'd probably rename pageNr to curIndex (or something like) at this point.
听起来您希望下一个是单个页面,而不是页面。在这种情况下,您可能希望跟踪当前索引并递增它。您可以通过对页面大小和起始页进行一些数学计算来完成此操作,但仅跟踪当前索引会简单得多。
另外,当 curIndex 是页面大小的倍数时,您只想更改起始页。
it sounds like you want the next to be a single, not a page. In that case you would probably want to keep track of the current index and increment it. You could do this by some math with the page size and start page, but it would be much simpler to just track the current index.
also, you would only want to change start page when curIndex is a multiple of page size.