Skip to content

Commit

Permalink
merging #179 + refactor, view 'attributes' for this.el
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 7, 2011
1 parent 512ff7e commit a5079ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 8 additions & 8 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,9 @@
// Cached regex to split keys for `delegate`.
var eventSplitter = /^(\w+)\s*(.*)$/;

// List of view options to be merged as properties.
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];

// Set up all inheritable **Backbone.View** properties and methods.
_.extend(Backbone.View.prototype, Backbone.Events, {

Expand Down Expand Up @@ -901,12 +904,10 @@
// attached directly to the view.
_configure : function(options) {
if (this.options) options = _.extend({}, this.options, options);
if (options.model) this.model = options.model;
if (options.collection) this.collection = options.collection;
if (options.el) this.el = options.el;
if (options.id) this.id = options.id;
if (options.className) this.className = options.className;
if (options.tagName) this.tagName = options.tagName;
for (var i = 0, l = viewOptions.length; i < l; i++) {
var attr = viewOptions[i];
if (options[attr]) this[attr] = options[attr];
}
this.options = options;
},

Expand All @@ -916,8 +917,7 @@
// an element from the `id`, `className` and `tagName` proeprties.
_ensureElement : function() {
if (!this.el) {
var attrs = {};
if (this.attributes) attrs = this.attributes;
var attrs = this.attributes || {};
if (this.id) attrs.id = this.id;
if (this.className) attrs['class'] = this.className;
this.el = this.make(this.tagName, attrs);
Expand Down
6 changes: 6 additions & 0 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ $(document).ready(function() {
ok(!view.el);
});

test("View: with attributes", function() {
var view = new Backbone.View({attributes : {'class': 'one', id: 'two'}});
equals(view.el.className, 'one');
equals(view.el.id, 'two');
});

test("View: multiple views per element", function() {
var count = 0, ViewClass = Backbone.View.extend({
el: $("body"),
Expand Down

0 comments on commit a5079ab

Please sign in to comment.