/index.html

https://github.com/tonyto/hello-backbone · HTML · 105 lines · 84 code · 21 blank · 0 comment · 0 complexity · 1b2eb34dcfe6eecc3bd6324969b112e1 MD5 · raw file

  1. <html>
  2. <head>
  3. <link rel='stylesheet' href='style.css'>
  4. <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
  5. <script type='text/javascript' src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
  6. <script type='text/javascript' src="backbone.js"></script>
  7. <script type='text/javascript' src="backbone-localstorage.js"></script>
  8. <script>
  9. $(document).ready(function() {
  10. window.Idea = Backbone.Model.extend({
  11. defaults: function() {
  12. return {
  13. createdOn: new Date()
  14. };
  15. },
  16. initialize: function() {
  17. console.log('hey Im new: ' + this.get('title'));
  18. }
  19. });
  20. window.IdeaList = Backbone.Collection.extend({
  21. model: Idea,
  22. localStorage: new Store("ideas"),
  23. });
  24. window.Ideas = new IdeaList();
  25. window.ItemView = Backbone.View.extend({
  26. el: $('.idea-list'),
  27. template: _.template($('#item_template').html()),
  28. initialize: function(item) {
  29. // Maps.create(item);
  30. this.model = item
  31. $('#new-idea').val('');
  32. this.render();
  33. },
  34. render: function() {
  35. console.log(this.model.toJSON());
  36. $(this.el).append(this.template(this.model.toJSON()));
  37. return this;
  38. },
  39. });
  40. window.AppView = Backbone.View.extend({
  41. el: $('#content'),
  42. template: _.template($('#scene_template').html()),
  43. initialize: function () {
  44. _.bind(this, "createOnEnter");
  45. },
  46. events: {
  47. "keypress #new-idea": "createOnEnter"
  48. },
  49. showToolTip: function(e) {
  50. this.$(".ui-tooltip").removeClass('hide');
  51. },
  52. createOnEnter: function (e) {
  53. var text = $('#new-idea').val();
  54. if (!text || e.keyCode != 13) return;
  55. var idea = new Idea({title: text});
  56. new ItemView(idea);
  57. }
  58. });
  59. window.App = new AppView;
  60. })
  61. </script>
  62. <head>
  63. <body>
  64. <h1>Idea Mapper</h1>
  65. <div id='content'>
  66. <div>
  67. <input type="text" id="new-idea" name="new-idea" placeholder="Give your idea a name"/>
  68. <span class="ui-tooltip hide"> Press enter to create this idea </span>
  69. </div>
  70. <div>
  71. <ul class='idea-list'>
  72. </ul>
  73. </div>
  74. </div>
  75. <script type='text/template' id='scene_template'>
  76. <h4>hello<h4>
  77. </script>
  78. <script type='text/template' id='item_template'>
  79. <li class="item">
  80. <%= title %>
  81. </li>
  82. </script>
  83. </body>
  84. </html>