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

/examples/login-example/public/js/views/employee/list.js

http://github.com/jspears/bobamo
JavaScript | 55 lines | 47 code | 7 blank | 1 comment | 5 complexity | cbd88d7289bba8d7b3ba4922abf82709 MD5 | raw file
Possible License(s): Apache-2.0
  1. define(['Backbone', 'jquery', 'underscore', 'collections/employee', 'text!tpl/employee-list-item.html'], function (Backbone, $, _, collection, employeeListItem) {
  2. var EmployeeListView = Backbone.View.extend({
  3. // el:'#content',
  4. tagName:'ul',
  5. className:'nav nav-list',
  6. initialize:function () {
  7. var self = this;
  8. if (!this.model){
  9. this.model = new collection.EmployeeCollection();
  10. this.model.fetch();
  11. }
  12. this.model.bind("reset", this.render, this);
  13. this.model.bind("add", function (employee) {
  14. console.log('add', employee);
  15. $(self.el).append(new EmployeeListItemView({model:employee}).render().el);
  16. });
  17. },
  18. render:function (obj) {
  19. if (this.options && this.options.container){
  20. var $c = $(this.options.container);
  21. $c.empty().append($(this.el));
  22. }
  23. $(this.el).empty();
  24. this.$el.append('<li><a class="btn btn-mini pull-right" href="#/employee/edit"><b class="icon-edit"/>Create Employee</a></li>')
  25. this.$el.append('<li style="clear:right"><br/></li>')
  26. _.each(this.model.models, function (employee) {
  27. $(this.el).append(new EmployeeListItemView({model:employee}).render().el);
  28. }, this);
  29. if(this.model.models.length == 0){
  30. this.$el.append('<li style="clear:right"><p class="alert alert-info">No Employees Found</p></li>');
  31. }
  32. return this;
  33. }
  34. });
  35. var EmployeeListItemView = Backbone.View.extend({
  36. tagName:"li",
  37. initialize:function () {
  38. this.template = _.template(employeeListItem);
  39. this.model.bind("change", this.render, this);
  40. this.model.bind("destroy", this.close, this);
  41. },
  42. render:function (eventName) {
  43. $(this.el).html(this.template(this.model.toJSON()));
  44. return this;
  45. }
  46. });
  47. return EmployeeListView;
  48. });