Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setAttributes for View #4128

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1448,11 +1448,8 @@
// an element from the `id`, `className` and `tagName` properties.
_ensureElement: function() {
if (!this.el) {
var attrs = _.extend({}, _.result(this, 'attributes'));
if (this.id) attrs.id = _.result(this, 'id');
if (this.className) attrs['class'] = _.result(this, 'className');
this.setElement(this._createElement(_.result(this, 'tagName')));
this._setAttributes(attrs);
this.setAttributes();
} else {
this.setElement(_.result(this, 'el'));
}
Expand All @@ -1462,6 +1459,19 @@
// subclasses using an alternative DOM manipulation API.
_setAttributes: function(attributes) {
this.$el.attr(attributes);
},

// Set attributes from the passed in argument or the view options
// on this view's element.
setAttributes: function(attrs) {
if (!attrs) {
attrs = _.extend({}, _.result(this, 'attributes'));
if (this.id) attrs.id = _.result(this, 'id');
if (this.className) attrs['class'] = _.result(this, 'className');
}

this._setAttributes(attrs);
return this;
}

});
Expand Down
56 changes: 51 additions & 5 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@
assert.expect(2);
var View = Backbone.View.extend({
attributes: {
'id': 'id',
'class': 'class'
id: 'id',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes to the quotes here fix a linter warning

class: 'class'
}
});

Expand All @@ -310,7 +310,7 @@
assert.expect(1);
var View = Backbone.View.extend({
attributes: function() {
return {'class': 'dynamic'};
return {class: 'dynamic'};
}
});

Expand All @@ -323,8 +323,8 @@
className: 'backboneClass',
id: 'backboneId',
attributes: {
'class': 'attributeClass',
'id': 'attributeId'
class: 'attributeClass',
id: 'attributeId'
}
});

Expand Down Expand Up @@ -513,4 +513,50 @@
assert.notEqual($oldEl, myView.$el);
});

QUnit.test('setAttributes', function(assert) {
assert.expect(4);
var View = Backbone.View.extend({
className: function() {
return this.foo ? 'fooClass' : 'backboneClass';
},
attributes: function() {
return {
class: 'attributeClass',
id: this.foo ? 'fooId' : 'attributeId'
};
}
});

var myView = new View;
assert.strictEqual(myView.el.className, 'backboneClass');
assert.strictEqual(myView.el.id, 'attributeId');
myView.foo = true;
myView.setAttributes();
assert.strictEqual(myView.el.className, 'fooClass');
assert.strictEqual(myView.el.id, 'fooId');
});

QUnit.test('setAttributes with attrs argument', function(assert) {
assert.expect(4);
var View = Backbone.View.extend({
className: function() {
return 'backboneClass';
},
attributes: {
class: 'attributeClass',
id: 'attributeId'
}
});

var myView = new View;
assert.strictEqual(myView.el.className, 'backboneClass');
assert.strictEqual(myView.el.id, 'attributeId');
myView.setAttributes({
class: 'setAttributeClass',
id: 'setAttributeId'
});
assert.strictEqual(myView.el.className, 'setAttributeClass');
assert.strictEqual(myView.el.id, 'setAttributeId');
});

})(QUnit);