PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Grails/backbone/web-app/js/application.js

https://github.com/Refactr/open-source
JavaScript | 203 lines | 121 code | 39 blank | 43 comment | 8 complexity | 346b69416b19b453ae50b63cbfb8631f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // usage: log('inside coolFunc',this,arguments);
  2. // paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
  3. window.log = function(){
  4. log.history = log.history || []; // store logs to an array for reference
  5. log.history.push(arguments);
  6. if(this.console){
  7. console.log( Array.prototype.slice.call(arguments) );
  8. }
  9. };
  10. // remap jQuery to $
  11. (function($){
  12. // MODELS
  13. var Movie = Backbone.Model.extend({
  14. initialize: function (spec) {
  15. if (!spec || !spec.title) {
  16. throw "InvalidConstructArgs";
  17. }
  18. // we may also want to store something else as an attribute
  19. // for example a unique ID we can use in the HTML to identify this
  20. // item's element. We can use the models 'cid' or 'client id for this'.
  21. this.set({
  22. htmlId: 'movie_' + this.cid
  23. })
  24. },
  25. validate: function (attrs) {
  26. if (attrs.title) {
  27. if (!_.isString(attrs.title) || attrs.title.length === 0 ) {
  28. log('Movie.validate', this, arguments);
  29. return "Title must be a string with a length";
  30. }
  31. }
  32. }
  33. });
  34. var MovieAppModel = Backbone.Model.extend({
  35. initialize: function () {
  36. log('MovieAppModel.initialize', this, arguments);
  37. // init and store our MovieCollection in our app object
  38. this.movies = new MovieLibrary();
  39. }
  40. });
  41. // COLLECTIONS
  42. var MovieLibrary = Backbone.Collection.extend({
  43. model: Movie,
  44. initialize: function () {
  45. // somthing
  46. }
  47. });
  48. // VIEWS
  49. var MovieView = Backbone.View.extend({
  50. initialize: function (args) {
  51. _.bindAll(this, 'changeTitle');
  52. log('MovieView.initialize', this, arguments)
  53. //log(this.model)
  54. this.model.bind('change:title', this.changeTitle);
  55. },
  56. events: {
  57. 'click .title': 'handleTitleClick'
  58. },
  59. render: function () {
  60. // "ich" is ICanHaz.js magic
  61. // this.el = ich.movie(this.model.toJSON());
  62. // this.el = this.model.toJSON();
  63. log('MovieView.render', this, arguments);
  64. //log(this.model.get('title'));
  65. var content = '<li id="'+this.model.get('htmlId')+'"><span class="title">'+this.model.get('title')+'</span> <a href="javascript:app.view.removeMovie(\''+this.model.get('htmlId')+'\');">x</span></li>';
  66. //log(content)
  67. this.el = $(content);
  68. //log(this.el);
  69. return this;
  70. },
  71. changeTitle: function () {
  72. this.$('.title').text(this.model.get('title'));
  73. },
  74. handleTitleClick: function () {
  75. alert('you clicked the title: ' + this.model.get('title'));
  76. }
  77. });
  78. var MovieAppView = Backbone.View.extend({
  79. initialize: function () {
  80. log('MovieAppView.initialize', this, arguments);
  81. // this.model refers the the model we pass to the view when we
  82. // first init our view. So here we listen for changes to the movie collection.
  83. this.model.movies.bind('add', this.addMovie);
  84. this.model.movies.bind('remove', this.removeMovie);
  85. },
  86. events: {
  87. // any user events (clicks etc) we want to respond to
  88. },
  89. // grab and populate our main template
  90. render: function () {
  91. // once again this is using ICanHaz.js, but you can use whatever
  92. //this.el = ich.app(this.model.toJSON());
  93. log('MovieAppView.render', this, arguments);
  94. this.el = $('<ul id="movies"></ul>');//this.model.toJSON();
  95. // store a reference to our movie list
  96. this.movieList = this.el.filter('#movies');
  97. return this;
  98. },
  99. addMovie: function (movie) {
  100. var view = new MovieView(movie);
  101. // here we use our stored reference to the movie list element and
  102. // append our rendered movie view.
  103. log('MovieAppView.addMovie', this, arguments);
  104. this.movieList.append(view.render().el);
  105. },
  106. removeMovie: function (movie) {
  107. // here we can use the html ID we stored to easily find
  108. // and remove the correct element/elements from the view if the
  109. // collection tells us it's been removed.
  110. log('MovieAppView.removeMovie')
  111. log(movie)
  112. var htmlId = (typeof movie.get !== 'undefined') ? movie.get('htmlId') : movie
  113. log(htmlId)
  114. this.$('#' + htmlId).remove();
  115. }
  116. });
  117. // CONTROLLERS
  118. var MovieAppController = {
  119. init: function (spec) {
  120. // default config
  121. this.config = {
  122. connect: true
  123. };
  124. // extend our default config with passed in object attributes
  125. _.extend(this.config, spec);
  126. this.model = new MovieAppModel({
  127. nick: this.config.nick,
  128. account: this.config.account,
  129. jid: this.config.jid,
  130. boshUrl: this.config.boshUrl
  131. });
  132. this.view = new MovieAppView({model: this.model});
  133. // standalone modules that respond to document events
  134. //this.sm = new SoundMachine();
  135. return this;
  136. },
  137. promptMovie: function() {
  138. var movie = prompt("Please enter a Movie:");
  139. this.view.addMovie({model:new Movie({title:movie})});
  140. },
  141. // any other functions here should be events handlers that respond to
  142. // document level events. In my case I was using this to respond to incoming XMPP
  143. // events. So the logic for knowing what those meant and creating or updating our
  144. // models and collections lived here.
  145. handlePubSubUpdate: function () {}
  146. };
  147. $(document).ready(function() {
  148. // init our app
  149. window.app = MovieAppController.init({
  150. account: '{{ user.get_profile.account.slug }}',
  151. // etc, etc
  152. });
  153. // draw our main view
  154. $('#view').append(app.view.render().el);
  155. })
  156. })(this.jQuery);
  157. // catch all document.write() calls
  158. (function(doc){
  159. var write = doc.write;
  160. doc.write = function(q){
  161. log('document.write(): ',arguments);
  162. if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);
  163. };
  164. })(document);