PageRenderTime 23ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/python/static/latent.js

http://github.com/saaperezru/Zentity
JavaScript | 123 lines | 113 code | 1 blank | 9 comment | 3 complexity | 720b4aef6b7e15f07d8cc2f85cc6eceb MD5 | raw file
  1. $(function(){
  2. // Topic Model
  3. // ----------
  4. window.Topic = Backbone.Model.extend({
  5. defaults: function() {
  6. return {
  7. selected: true,
  8. imgId: 0,
  9. };
  10. },
  11. toggle: function() {
  12. this.save({selected: !this.get("selected")});
  13. }
  14. });
  15. // Topic Collection
  16. // ---------------
  17. window.TopicCollection = Backbone.Collection.extend({
  18. model: Topic,
  19. localStorage: new Store("topics"),
  20. selected: function() {
  21. return this.filter(function(topic){ return topic.get('selected'); });
  22. },
  23. remaining: function() {
  24. return this.without.apply(this, this.selected());
  25. },
  26. });
  27. window.Topics = new TopicCollection;
  28. // Topic Item View
  29. // --------------
  30. window.TopicView = Backbone.View.extend({
  31. tagName: "li",
  32. template: _.template(
  33. "<div class=\"notice alert-message block-message <%= selected ? 'success' : 'warning' %>\"> \
  34. <div class=\"innertopic row\"> \
  35. <h2><%= name %> <small><%= type %></small></h2> \
  36. <ul class=\"media-grid\"> \
  37. <% _.each(docs, function(img) { %> <li><a><img class='thumbnail' src='/images/get?id=<%= img %>'></a></li> <% }); %> \
  38. </ul> \
  39. <div class=\"clearfix\"></div> \
  40. <div class='input-prepend'> \
  41. <small> select </small><input type=\"checkbox\" <%= selected ? 'checked' : '' %>/> \
  42. </div> \
  43. </div> \
  44. </div>"
  45. ),
  46. events: {
  47. "click input" : "toggleSelected",
  48. },
  49. initialize: function() {
  50. this.model.bind('change', this.render, this);
  51. this.model.bind('destroy', this.remove, this);
  52. },
  53. render: function() {
  54. $(this.el).html(this.template(this.model.toJSON()));
  55. return this;
  56. },
  57. toggleSelected: function() {
  58. var model = this.model;
  59. $.ajax({
  60. url : "/latentTopics/select?id=" + this.model.get("lid") + "&s=" + !this.model.get("selected") + "&type=" + this.model.get("type"),
  61. success: function(response){
  62. if(response.error) {
  63. console.log(data.error);
  64. }
  65. model.toggle();
  66. },
  67. });
  68. },
  69. remove: function() {
  70. $(this.el).remove();
  71. },
  72. clear: function() {
  73. this.model.destroy();
  74. }
  75. });
  76. // The Application
  77. // ---------------
  78. window.AppView = Backbone.View.extend({
  79. el : $('#lapp'),
  80. events : {
  81. },
  82. initialize: function(){
  83. Topics.bind('add', this.loadOne, this);
  84. Topics.bind('reset', this.loadAll, this);
  85. Topics.bind('all', this.render, this);
  86. this.createTopics();
  87. },
  88. render: function() {
  89. },
  90. loadOne :function(topic) {
  91. var view = new TopicView({model:topic});
  92. this.$('#topic-list').append(view.render().el);
  93. },
  94. loadAll :function() {
  95. Topics.each(this.loadOne)
  96. },
  97. createTopics : function() {
  98. $.ajax({
  99. url : "/latentTopics/list",
  100. success: function(response){
  101. if(response.error) {
  102. console.log(data.error);
  103. }
  104. if(response.LTS) {
  105. console.log(response);
  106. _.each(response.LTS,function(topic){
  107. Topics.create({
  108. selected : topic.selected,
  109. lid : topic.lid,
  110. type : topic.type,
  111. docs : topic.docs,
  112. name : topic.name,
  113. });
  114. });
  115. }
  116. }
  117. });
  118. },
  119. });
  120. // Finally, we kick things off by creating the **App**.
  121. window.App = new AppView;
  122. });