1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/UI/Mixins/backbone.modelbinder.mixin.js
kay.one 979efb8354 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
2013-04-20 11:59:19 -07:00

53 lines
1.4 KiB
JavaScript

'use strict';
var oldItemViewRender = Marionette.ItemView.prototype.render;
var oldItemCollectionViewRender = Marionette.CollectionView.prototype.render;
Marionette.View.prototype.viewName = function () {
if (this.template) {
var regex = new RegExp('\/', 'g');
return this.template
.toLocaleLowerCase()
.replace('template', '')
.replace(regex, '-');
}
return undefined;
};
Marionette.ItemView.prototype.render = function () {
var result = oldItemViewRender.apply(this, arguments);
//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;
};
Marionette.CollectionView.prototype.render = function () {
if (this.model) {
NzbDrone.ModelBinder.bind(this.model, this.el);
}
return oldItemCollectionViewRender.apply(this, arguments);
};