比这篇新的文章: 水桶倒水问题求解
比这篇旧的文章: 48 小时编写sheme解释器的学习笔记-stdlib.scm

jquery douban demo: book list

语言: JavaScript, 标签: demo api douban jquery 2008/11/27发布 1年前更新 更新记录
作者: wuyuntao, 点击736次, 评论(0), 收藏者(1), , 打分:

背景
主题: 字体:
01 // Main entry
02 $(function() {
03     if (!window.google || !google.gears) return;
04
05     // Initialize UI
06     $('#warning').hide();
07     $('#username').attr('disabled', false);
08     $('#submit').attr('disabled', false);
09     $('#show_code').click(function() {
10         $('#code').toggle();
11         return false;
12     });
13
14     // Initialize douban service
15     $.douban.http.register('gears', gearsHandler);
16     var service = $.douban('service', {
17         key: '0107c5c3c9d4ecc40317514b5d7ec64c',
18         secret: '7feaf4ec7b6989f8',
19         type: 'gears'
20     });
21
22     // Handler submissions
23     $('#get_username_form').submit(function() {
24         var username = $('#username').val();
25         if (!username.length) return false;
26
27         // Get the first ten book collections for user
28         service.collection.getForUser(username, 0, 10, function(books) {
29
30             // If returns any result, display them
31             if (books.total && books.total > 0) {
32                 $('#result tbody').empty();
33
34                 // Insert each book into result area
35                 $.each(books.entries, function() {
36                     insertBook(this.subject);
37                 });
38             } else {
39                 alert('这个用户不存在或这个用户未收藏任何书目');
40             }
41
42             // Hide loading
43             $('#loading').hide();
44         }, 'book');
45
46         // Show Loading
47         $('#loading').show();
48         return false;
49     });
50 });
51
52 function insertBook(book) {
53     var result = $('#result tbody');
54     var tmpl = '<tr id="{ID}" class="book"><td class="cover"><img src="{IMAGE_URL}" /></td><td class="info"><span class="title"><a href="{URL}">{TITLE}</a></span><br /><span class="author">作者:{AUTHORS}</span></td></tr>';
55     var html = tmpl.replace(/\{ID\}/, book.id.match(/(\d+)/)[1])
56                    .replace(/\{IMAGE_URL\}/, book.imageUrl)
57                    .replace(/\{URL\}/, book.url)
58                    .replace(/\{TITLE\}/, book.title)
59                    .replace(/\{AUTHORS\}/, book.authors.join('、'));
60     $(html).appendTo(result);
61 }


所有评论,共0条:( 我也来说两句)


发表评论

注册登录后再发表评论