PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/static/js/vendor/jsoma-tabletop-32ee2bb/examples/backbone/backbone.html

https://bitbucket.org/bloodearnest/gateway-website
HTML | 108 lines | 93 code | 15 blank | 0 comment | 0 complexity | fe7b4b66236fb1755247c178a9904e86 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link href="../common/cats.css" media="screen" rel="stylesheet" type="text/css" />
  5. </head>
  6. <body>
  7. <h1>A Backbone.js example about cats I've met</h1>
  8. <marquee><p>As you can tell, you can make a very complicated web page with this stuff.</p></marquee>
  9. <script id="cat-template" type="text/underscore-template">
  10. <div class="entry">
  11. <h2><%= name %>!</h2>
  12. <h3>age <%= age %></h3>
  13. <div class="body">
  14. <%= description %>
  15. </div>
  16. </div>
  17. </script>
  18. <div id="content"></div>
  19. <script type="text/javascript" src="../common/jquery.js"></script>
  20. <script type="text/javascript" src="../common/underscore.js"></script>
  21. <script type="text/javascript" src="backbone.js"></script>
  22. <script type="text/javascript" src="../../src/backbone.tabletopSync.js"></script>
  23. <script type="text/javascript" src="../../src/tabletop.js"></script>
  24. <script type="text/javascript">
  25. var public_spreadsheet_url = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdE5OcDE1SENpT1g2R2JEX2tnZ3ZIWHc&output=html';
  26. /*
  27. You need to declare the tabletop instance separately, then feed it into the model/collection
  28. You *must* specify wait: true so that it doesn't try to fetch when you initialize
  29. */
  30. var storage = Tabletop.init( { key: public_spreadsheet_url,
  31. wait: true } )
  32. /*
  33. Need to specify that you'd like to sync using Backbone.tabletopSync
  34. Can specify tabletop attributes, or you can do it on the collection
  35. */
  36. var Cat = Backbone.Model.extend({
  37. idAttribute: 'name',
  38. tabletop: {
  39. instance: storage,
  40. sheet: 'Cats'
  41. },
  42. sync: Backbone.tabletopSync
  43. })
  44. /*
  45. Need to specify that you'd like to sync using Backbone.tabletopSync
  46. Need to specify a tabletop key and sheet
  47. */
  48. var CatCollection = Backbone.Collection.extend({
  49. // Reference to this collection's model.
  50. model: Cat,
  51. tabletop: {
  52. instance: storage,
  53. sheet: 'Cats'
  54. },
  55. sync: Backbone.tabletopSync
  56. });
  57. var CatView = Backbone.View.extend({
  58. tagname: 'div',
  59. template: _.template($('#cat-template').html()),
  60. render: function() {
  61. $(this.el).html(this.template(this.model.toJSON()));
  62. return this;
  63. }
  64. })
  65. </script>
  66. <script type="text/javascript">
  67. /*
  68. You need to initialize Tabletop before you do aaaaanything.
  69. You might think it'd be a good idea to put that into backbone.tabletopSync,
  70. but IMHO the fact that you could put the key/url into any model anywhere
  71. ever sounds like trouble.
  72. */
  73. $(document).ready( function() {
  74. var cats = new CatCollection();
  75. cats.fetch({ success: showInfo });
  76. });
  77. function showInfo(cats) {
  78. var bosco_view = new CatView({ model: cats.get('Bosco') });
  79. $("#content").append( bosco_view.render().el );
  80. /*
  81. Fetching on models works as long as you've specified a sheet
  82. and an idAttribute for the Backbone.Model (you can always
  83. use rowNumber, it comes baked in to Tabletop)
  84. */
  85. thomas = new Cat({name: 'Thomas'})
  86. thomas.fetch();
  87. var thomas_view = new CatView({ model: thomas });
  88. $("#content").append( thomas_view.render().el );
  89. }
  90. document.write("The published spreadsheet is located at <a target='_new' href='" + public_spreadsheet_url + "'>" + public_spreadsheet_url + "</a>");
  91. </script>
  92. </body>
  93. </html>