我正在创建一个插件,它可以从 soundcloud 或用户自己的列表中加载播放列表信息。为此,我设置了如果选项中有一个名为“播放列表”的数组,它将加载该信息。下面是代码:
<script type="text/javascript">
$(document).ready(function() {
$('.test').Tarabu({
soundcloud_user: '',
soundcloud_clientId: "bcc776e7aa65dbc29c40ff21a1a94ecd",
playlist: [
{
title:"Cro Magnon Man",
artist:"The Stark Palace",
mp3:"http://www.jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3",
oga:"http://www.jplayer.org/audio/ogg/TSP-01-Cro_magnon_man.ogg",
poster: "http://www.jplayer.org/audio/poster/The_Stark_Palace_640x360.png"
},{
title:"Cro Magnon Man",
artist:"The Stark Palace",
mp3:"http://www.jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3",
oga:"http://www.jplayer.org/audio/ogg/TSP-01-Cro_magnon_man.ogg",
poster: "http://www.jplayer.org/audio/poster/The_Stark_Palace_640x360.png"
}
]
});
});
(function($) {
$.fn.Tarabu = function(options) {
$.fn.Tarabu.defaults = {
soundcloud_user: null,
soundcloud_clientId: null,
playlist: 'soundcloud'
};
var o = $.extend({}, $.fn.Tarabu.defaults, options);
return this.each(function() {
//Determine playlist type. If soundcloud is set as the playlist it will load either the complete user set (soundcloud_user must be set) or selected sets. If local playlists have been set it will load them.
if (o.playlist == 'soundcloud') {
alert('soundcloud');
//Determine if the soundcloud API client ID is set. This is required to access json data.
if(typeof(o.soundcloud_clientId) != "undefined" && o.soundcloud_clientId !== null) {
//Determine if the user is set and then load all sets.
if(typeof(o.soundcloud_user) != "undefined" && o.soundcloud_user !== null) {
alert('soundcloud user set');
$.getJSON('http://api.soundcloud.com/users/'+ o.soundcloud_user +'/playlists.json?client_id=' + o.soundcloud_clientId, { get_param: 'value' },
function(playlists_data) {
$.each(playlists_data, function(index, playlists) {
//Get playlists data
i = 1;
$.getJSON(playlists.uri + '.json?client_id=' + o.soundcloud_clientId, { get_param: 'value' }, function(playlist) {
$("<div class='player row' id='id" + i +"'><div class=' four columns'><div class='artwork'><img /></div></div><div class='playlist eight columns'><div class='description'></div><ol class='tracks'></ol></div></div>").appendTo('.soundcloud');
//get track data
$.each(playlist.tracks, function(index, track) {
// Create a list item for track, associate it's data, and append it to the track list.
var $li = $('<li class="track_' + track.id + '">' + (index + 1) + '. ' + track.title + '</li>').data('track', track).appendTo('#id'+i+' .tracks');
// Find the appropriate stream url depending on whether the track has a secret_token or is public.
url = track.stream_url;
(url.indexOf("secret_token") == -1) ? url = url + '?' : url = url + '&';
url = url + 'client_id=' + o.client_id;
});
i++
});
});
});
} else {
alert('soundcloud not user set');
}
我一直在尝试以与提取 soundcloud json 的方式类似的方式加载信息,但是收效甚微。我的问题是,在语义上这样做的最佳方法是什么?
为了方便起见,我在这里创建了一个 jsfiddle,但是由于代理问题,脚本无法正常运行