From 979efb8354b3e174a1265ba300bef210d24ad70f Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 20 Apr 2013 11:59:01 -0700 Subject: [PATCH] much smarter binding, auto-binding is only done when the element has binding attributes ('name') also now an error is throw in there are binding elements but view doesn't have a model --- UI/Mixins/backbone.marionette.templates.js | 1 - UI/Mixins/backbone.modelbinder.mixin.js | 21 +++++++++++++++++---- UI/app.js | 2 -- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/UI/Mixins/backbone.marionette.templates.js b/UI/Mixins/backbone.marionette.templates.js index 9acb2c67b..876cbd58b 100644 --- a/UI/Mixins/backbone.marionette.templates.js +++ b/UI/Mixins/backbone.marionette.templates.js @@ -13,7 +13,6 @@ Marionette.TemplateCache.get = function (templateId) { return function (data) { try { - //console.log('rendering template ' + templateKey); return templateFunction(data); } catch (error) { diff --git a/UI/Mixins/backbone.modelbinder.mixin.js b/UI/Mixins/backbone.modelbinder.mixin.js index a43aeec04..91d610ca0 100644 --- a/UI/Mixins/backbone.modelbinder.mixin.js +++ b/UI/Mixins/backbone.modelbinder.mixin.js @@ -10,7 +10,7 @@ Marionette.View.prototype.viewName = function () { return this.template .toLocaleLowerCase() - .replace('template','') + .replace('template', '') .replace(regex, '-'); } @@ -21,12 +21,25 @@ Marionette.ItemView.prototype.render = function () { var result = oldItemViewRender.apply(this, arguments); - this.$el.addClass('iv-' + this.viewName()); - if (this.model) { - NzbDrone.ModelBinder.bind(this.model, this.el); + + //check to see if el has bindings (name attribute) + // any element that has a name attribute and isn't child of another view. + if (this.$('[name]').not("[class*='iv-'] [name]").length > 0) { + if (!this.model) { + throw 'view ' + this.viewName() + ' has binding attributes but model is not defined'; + } + + if (!this._modelBinder) { + this._modelBinder = new Backbone.ModelBinder(); + } + + window.console.log('binding ' + this.viewName()); + + this._modelBinder.bind(this.model, this.el); } + this.$el.addClass('iv-' + this.viewName()); return result; }; diff --git a/UI/app.js b/UI/app.js index ed4f9d567..ff7c250aa 100644 --- a/UI/app.js +++ b/UI/app.js @@ -71,8 +71,6 @@ define('app', function () { console.log('starting application'); - NzbDrone.ModelBinder = new Backbone.ModelBinder(); - //TODO: move this out of here Handlebars.registerHelper("formatStatus", function (status, monitored) { if (!monitored) {