Framework7+Vue.js Spotify播放器 - 實例詳解(2)

參考:Framework7+Vue.js Spotify播放器 - 實例詳解(1)
回顧一下:

  1. 用Template模板初始化
  2. 添加 Font Awesome Icon 圖標庫
  3. Framework7 View和Page概念
  4. 更新main視圖
  5. 自定義樣式文件CSS
  6. 初始化App應用怨愤,關聯(lián)slider和顯示的數(shù)值

7. 調用Spotify API挣饥,處理返回數(shù)據(jù)

App的主界面已經(jīng)寫好经宏,下面開始處理數(shù)據(jù)充石。
Spotify是國外流行的一個歌曲搜索平臺灭袁,提供API訪問瘦馍。

  • 給Button添加一個@click方法歹撒,以ajax訪問API,然后處理返回數(shù)據(jù)
  • 給搜索框v-model綁定搜索條件
  • 通過window.f7.alert捍壤,輕松顯示提示框(太方便了骤视,就跟原生js alert('abc');一樣,一行就實現(xiàn)了>榫酢)
  • 通過window.Dom7.ajax专酗,輕松實現(xiàn)Ajax訪問 -- 開始體會到F7的強大了吧?盗扇!
  • TODO: ajax訪問時祷肯,顯示旋轉的等待圖案,你來試一試疗隶。(ref: http://framework7.io/docs/modal.html#preloader
# \spotifyApp\src\main.vue
<template>
  佑笋。。斑鼻。
  <f7-input type="text" placeholder="Search for..." v-model="term" />
  蒋纬。。坚弱。
  <f7-button id="btnSearch" class="button button-big button-fill color-green" @click="onSubmit">Submit</f7-button>
  蜀备。。荒叶。
</template>
<script>
export default {
  name: 'Index',
  data() {
    return {
      sliderVal: 20,
      term: '',
    }
  },
  mounted() {
    window.Dom7(document).on('deviceready', () => {
      console.log("Device is ready!");
    });
  },
  methods: {
    onSubmit() {
      if (this.term.length == 0) {
        window.f7.alert("請輸入搜索內容");
      } else {
        var mediaType = "track";
        var url = "https://api.spotify.com/v1/search?q=" + this.term + "&type=" + mediaType + "&limit=" + this.sliderVal;
        window.Dom7.ajax({
          dataType: 'json',
          url: url,
          success: function(resp) {
            window.f7.alert("Number of results " + resp.tracks.items.length);
          },
          error: function(xhr) {
            console.log("Error on ajax call " + xhr);
          }
        });
      }
    }
  }
}
</script>

保存碾阁,刷新一下Chrome瀏覽器,點擊“Submit”:

Paste_Image.png

輸入一個關鍵字些楣,再點擊“Submit”:

Paste_Image.png

OK脂凶,數(shù)據(jù)順利取回了,下面愁茁,我們創(chuàng)建一個新的Page蚕钦,來顯示返回的數(shù)據(jù)

8. 新的List Page(搜索結果)

創(chuàng)建新的List.vue文件,用v-for優(yōu)雅地展示搜索結果數(shù)據(jù)鹅很。

  • 子頁面里冠桃,f7-navbar是在f7-page內部!保證了整體navbar-through的效果
  • 使用store.js道宅,在page之間傳送數(shù)據(jù)(大型應用建議用vuex)
  • f7-searchbar輕松創(chuàng)建搜索框食听,對f7-list.searchbar-found里的數(shù)據(jù)胸蛛,能夠自動實現(xiàn)搜索功能
  • f7-list-item.swipeout對于每一條結果,實現(xiàn)左滑樱报、右滑(功能后面實現(xiàn))
  • v-for 請?zhí)砑?:key="item.id"葬项,不然vue會告警
  • Tips: Chrome瀏覽器模擬手機訪問時,有時頁面不能刷新迹蛤,需要關掉Chrome重開民珍。建議Chrome瀏覽器保持以網(wǎng)頁模式訪問
# \spotifyApp\src\assets\vue\List.vue
<template>
  <f7-page name="list" id="list">
    <f7-navbar>
      <f7-nav-left back-link="Back" sliding></f7-nav-left>
      <f7-nav-center sliding>Results</f7-nav-center>
      <f7-nav-right>
        <f7-link icon="icon-bars" open-panel=""></f7-link>
      </f7-nav-right>
    </f7-navbar>
      <!-- Search bar -->
      <f7-searchbar cancel-link="Cancel" search-list="#mediaList" placeholder="Search in items" :clear-button="true">
      </f7-searchbar>
      <!-- Will be visible if there is no any results found, defined by "searchbar-not-found" class -->
      <f7-list class="searchbar-not-found">
        <f7-list-item title="Nothing found"></f7-list-item>
      </f7-list>
      <!-- Search-through list -->
      <f7-list class="searchbar-found" media-list id="mediaList">
        <f7-list-item swipeout v-for="(item, index) in searchTracks" :key="item.id" link="#" :media="'![]( + item.album.images[2].url +)'" :title="item.name" :subtitle="item.artists[0].name" :text="item.album.name">
          <f7-swipeout-actions right>
            <f7-swipeout-button>
              <a class="bg-orange favorite" :data-item="index"><i class="icon fa fa-star fa-2x"></i></a>
              <a href="#" class="bg-blue share" :data-item="index"><i class="icon fa fa-share fa-2x"></i></a>
            </f7-swipeout-button>
          </f7-swipeout-actions>
          <f7-swipeout-actions left>
            <f7-swipeout-button>
              <a href="#" class="bg-green preview" :data-item="index"><i class="icon fa fa-play fa-2x"></i></a>
            </f7-swipeout-button>
          </f7-swipeout-actions>
        </f7-list-item>
      </f7-list>
  </f7-page>
</template>
<script>
import store from '../../store.js'
export default {
  name: 'list',
  data() {
    return {
      searchTracks: [],
    }
  },
  mounted() {
    window.Dom7(document).on('deviceready', () => {
      console.log("List Page is ready!");
      this.searchTracks = store.searchTracks;
    });
  }
}
</script>

數(shù)據(jù)中轉文件store.js,保存搜索結果:

# \spotifyApp\src\store.js
const store = {
    searchTracks: [],
};
export default store;

把List.vue加入路由routes.js:

# \spotifyApp\src\routes.js
export default [
  {
    path: '/list/',
    component: require('./assets/vue/List.vue')
  }
]

主頁面main.vue盗飒,點擊Submit后

  • 先把返回數(shù)據(jù)存入store: store.searchTracks = resp.tracks.items;
  • 通過F7路由嚷量,加載List.vue。路由跳轉: window.f7.mainView.router.load()
# \spotifyApp\src\main.vue
  methods: {
    onSubmit() {
      if (this.term.length == 0) {
        window.f7.alert("請輸入搜索內容");
      } else {
        var mediaType = "track";
        var url = "https://api.spotify.com/v1/search?q=" + this.term + "&type=" + mediaType + "&limit=" + this.sliderVal;;
        window.Dom7.ajax({
          dataType: 'json',
          url: url,
          success: function(resp) {
            // window.f7.alert("Number of results " + resp.tracks.items.length);
            console.log(resp.tracks.items);
            store.searchTracks = resp.tracks.items;
            window.f7.mainView.router.load({
              url: '/list/',
              context: resp.tracks.items
         });
          },
          error: function(xhr) {
            console.log("Error on ajax call " + xhr);
          }
        });
      }
    }

OK逆趣,數(shù)據(jù)展示頁面好了蝶溶,你應該能看到如下搜索結果圖:

Paste_Image.png

而且,不用寫一行js代碼宣渗,你的搜索框就已經(jīng)工作了:

Paste_Image.png

9. media頁面(歌曲詳情)

當我們點擊搜索結果里某一首歌曲抖所,需要打開歌曲詳情頁面,并且能夠播放

--> 請看詳解(3)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末痕囱,一起剝皮案震驚了整個濱河市田轧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鞍恢,老刑警劉巖傻粘,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異帮掉,居然都是意外死亡抹腿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門旭寿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人崇败,你說我怎么就攤上這事盅称。” “怎么了后室?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵缩膝,是天一觀的道長。 經(jīng)常有香客問我岸霹,道長疾层,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任贡避,我火速辦了婚禮痛黎,結果婚禮上予弧,老公的妹妹穿的比我還像新娘。我一直安慰自己湖饱,他們只是感情好掖蛤,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著井厌,像睡著了一般蚓庭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上仅仆,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天器赞,我揣著相機與錄音,去河邊找鬼墓拜。 笑死港柜,一個胖子當著我的面吹牛,可吹牛的內容都是我干的撮弧。 我是一名探鬼主播潘懊,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贿衍!你這毒婦竟也來了授舟?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贸辈,失蹤者是張志新(化名)和其女友劉穎释树,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體擎淤,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡奢啥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了嘴拢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片桩盲。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖席吴,靈堂內的尸體忽然破棺而出赌结,到底是詐尸還是另有隱情,我是刑警寧澤孝冒,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布柬姚,位于F島的核電站,受9級特大地震影響庄涡,放射性物質發(fā)生泄漏量承。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望撕捍。 院中可真熱鬧拿穴,春花似錦、人聲如沸卦洽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阀蒂。三九已至该窗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蚤霞,已是汗流浹背酗失。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留昧绣,地道東北人规肴。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像夜畴,于是被迫代替她去往敵國和親拖刃。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內容