|
1379 | 1379 | });
|
1380 | 1380 |
|
1381 | 1381 | test('Polymorphic models work with "advanced" constructors', function () {
|
1382 |
| - var A = Backbone.Model.extend(); |
1383 |
| - var B = Backbone.Model.extend(); |
| 1382 | + var A = Backbone.Model.extend({idAttribute: '_id'}); |
| 1383 | + var B = Backbone.Model.extend({idAttribute: '_id'}); |
1384 | 1384 | var C = Backbone.Collection.extend({
|
1385 | 1385 | model: Backbone.Model.extend({
|
1386 | 1386 | constructor: function (attrs) {
|
|
1393 | 1393 | var collection = new C([{_id: 1, type: 'a'}, {_id: 2, type: 'b'}]);
|
1394 | 1394 | equal(collection.length, 2);
|
1395 | 1395 | ok(collection.at(0) instanceof A);
|
| 1396 | + equal(collection.at(0).id, 1); |
1396 | 1397 | ok(collection.at(1) instanceof B);
|
| 1398 | + equal(collection.at(1).id, 2); |
1397 | 1399 |
|
| 1400 | + A.prototype.generateId = B.prototype.generateId = function (attrs) { |
| 1401 | + return attrs.type + '-' + attrs.id; |
| 1402 | + } |
1398 | 1403 | C = Backbone.Collection.extend({
|
1399 | 1404 | model: Backbone.Model.extend({
|
1400 | 1405 | constructor: function (attrs) {
|
1401 | 1406 | return attrs.type === 'a' ? new A(attrs) : new B(attrs);
|
1402 | 1407 | },
|
1403 | 1408 |
|
1404 |
| - generateId: function (attrs) { |
1405 |
| - return attrs.type + '-' + attrs.id; |
1406 |
| - } |
| 1409 | + generateId: A.prototype.generateId |
1407 | 1410 | })
|
1408 | 1411 | });
|
1409 | 1412 | collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]);
|
1410 | 1413 | equal(collection.length, 2);
|
1411 | 1414 | ok(collection.at(0) instanceof A);
|
| 1415 | + equal(collection.at(0).id, 'a-1'); |
1412 | 1416 | ok(collection.at(1) instanceof B);
|
| 1417 | + equal(collection.at(1).id, 'b-1'); |
1413 | 1418 | });
|
1414 | 1419 | })();
|
0 commit comments