-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Avoid Object.prototype collisions with defaults #3843
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,12 @@ | |
assert.equal(model.collection, collection); | ||
}); | ||
|
||
QUnit.test('Object.prototype properties are overridden by attributes', function(assert) { | ||
assert.expect(1); | ||
var model = new Backbone.Model({hasOwnProperty: true}); | ||
assert.equal(model.get('hasOwnProperty'), true); | ||
}); | ||
|
||
QUnit.test('initialize with attributes and options', function(assert) { | ||
assert.expect(1); | ||
var Model = Backbone.Model.extend({ | ||
|
@@ -57,19 +63,6 @@ | |
assert.equal(model.get('value'), 2); | ||
}); | ||
|
||
QUnit.test('initialize with defaults', function(assert) { | ||
assert.expect(2); | ||
var Model = Backbone.Model.extend({ | ||
defaults: { | ||
firstName: 'Unknown', | ||
lastName: 'Unknown' | ||
} | ||
}); | ||
var model = new Model({'firstName': 'John'}); | ||
assert.equal(model.get('firstName'), 'John'); | ||
assert.equal(model.get('lastName'), 'Unknown'); | ||
}); | ||
|
||
QUnit.test('parse can return null', function(assert) { | ||
assert.expect(1); | ||
var Model = Backbone.Model.extend({ | ||
|
@@ -428,7 +421,7 @@ | |
}); | ||
|
||
QUnit.test('defaults', function(assert) { | ||
assert.expect(4); | ||
assert.expect(9); | ||
var Defaulted = Backbone.Model.extend({ | ||
defaults: { | ||
one: 1, | ||
|
@@ -438,6 +431,9 @@ | |
var model = new Defaulted({two: undefined}); | ||
assert.equal(model.get('one'), 1); | ||
assert.equal(model.get('two'), 2); | ||
model = new Defaulted({two: 3}); | ||
assert.equal(model.get('one'), 1); | ||
assert.equal(model.get('two'), 3); | ||
Defaulted = Backbone.Model.extend({ | ||
defaults: function() { | ||
return { | ||
|
@@ -449,6 +445,15 @@ | |
model = new Defaulted({two: undefined}); | ||
assert.equal(model.get('one'), 3); | ||
assert.equal(model.get('two'), 4); | ||
Defaulted = Backbone.Model.extend({ | ||
defaults: {hasOwnProperty: true} | ||
}); | ||
model = new Defaulted(); | ||
assert.equal(model.get('hasOwnProperty'), true); | ||
model = new Defaulted({hasOwnProperty: undefined}); | ||
assert.equal(model.get('hasOwnProperty'), true); | ||
model = new Defaulted({hasOwnProperty: false}); | ||
assert.equal(model.get('hasOwnProperty'), false); | ||
}); | ||
|
||
QUnit.test('change, hasChanged, changedAttributes, previous, previousAttributes', function(assert) { | ||
|
@@ -989,8 +994,8 @@ | |
|
||
QUnit.test('`previous` for falsey keys', function(assert) { | ||
assert.expect(2); | ||
var model = new Backbone.Model({0: true, '': true}); | ||
model.set({0: false, '': false}, {silent: true}); | ||
var model = new Backbone.Model({'0': true, '': true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whys this needed? JS automatically casts keys to strings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was a liter warning for inconsistent string usage. Must have committed it by accident. 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Linting rules are not perfect 😞 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All good, they're much better now than before. |
||
model.set({'0': false, '': false}, {silent: true}); | ||
assert.equal(model.previous(0), true); | ||
assert.equal(model.previous(''), true); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why'd you get rid of this test case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's testing the same thing as the one above it, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No... it's testing that passing
undefined
is thrown away, not that the key isn't passed at all.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that this test?
With my "defaults" implementation:
The only difference with mine is it won't respect properties that are already on the
dest
object:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, missed that in the diff.
Then I'm confused. That changes _.default's behavior completely, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're only using defaults with an "empty" destination object, so it works the same. Externally, this wouldn't be acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but I dunno. Either we should fix this in Underscore or we shouldn't fix this at all. What's good for the goose is good for the gander...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we made it return a new instance instead of modifying the destination, it would work perfectly. We're kind of headed down that path with jashkenas/underscore#2232.