From aa4f5ba5fbf0d0f2b5ade3cd771270061e3ab4ba Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Mon, 20 May 2013 20:17:07 +0100 Subject: [PATCH] Avoid re-creating the member list elements on every change --- client/assets/dev/model_member.js | 1 + client/assets/dev/view.js | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/client/assets/dev/model_member.js b/client/assets/dev/model_member.js index 22fb2d6..bc2fa60 100644 --- a/client/assets/dev/model_member.js +++ b/client/assets/dev/model_member.js @@ -32,6 +32,7 @@ _kiwi.model.Member = Backbone.Model.extend({ this.sortModes(modes); this.set({"nick": nick, "modes": modes, "prefix": this.getPrefix(modes)}, {silent: true}); this.isOp(); + this.view = new _kiwi.view.Member({"model": this}); }, addMode: function (mode) { var modes_to_add = mode.split(''), diff --git a/client/assets/dev/view.js b/client/assets/dev/view.js index 1a763af..11b2f9f 100644 --- a/client/assets/dev/view.js +++ b/client/assets/dev/view.js @@ -1,5 +1,21 @@ /*jslint white:true, regexp: true, nomen: true, devel: true, undef: true, browser: true, continue: true, sloppy: true, forin: true, newcap: true, plusplus: true, maxerr: 50, indent: 4 */ /*global kiwi */ +_kiwi.view.Member = Backbone.View.extend({ + tagName: "li", + initialize: function (options) { + this.model.bind('change', this.render, this); + this.render(); + }, + render: function () { + var $this = this.$el, + prefix_css_class = (this.model.get('modes') || []).join(' '); + + $this.addClass('mode ' + prefix_css_class); + $this.html('' + this.model.get("prefix") + '' + this.model.get("nick") + ''); + $this.data('member', this.model); + return this; + } +}); _kiwi.view.MemberList = Backbone.View.extend({ tagName: "ul", @@ -11,20 +27,18 @@ _kiwi.view.MemberList = Backbone.View.extend({ $(this.el).appendTo('#kiwi .memberlists'); }, render: function () { - var $this = $(this.el); + var $this = this.$el; $this.empty(); this.model.forEach(function (member) { - var prefix_css_class = (member.get('modes') || []).join(' '); - $('
  • ' + member.get("prefix") + '' + member.get("nick") + '
  • ') - .appendTo($this) - .data('member', member); + $this.append(member.view.el); }); + return this; }, nickClick: function (event) { var $target = $(event.currentTarget).parent('li'), member = $target.data('member'), userbox; - + event.stopPropagation(); // If the userbox already exists here, hide it -- 2.25.1