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

Support for subModelTypes as class references #509

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@
* @return {Object}
*/
getObjectByName: function( name ) {
if (name.prototype instanceof Backbone.RelationalModel) {
return name;
}

var parts = name.split( '.' ),
type = null;

Expand Down
25 changes: 25 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,31 @@ $(document).ready(function() {
delete window.Carnivore;
});

test( "Inheritance with `subModelTypes` with class references and restore from json", function() {
var Mammal = Animal.extend({
subModelTypes: {},
defaults: {title: 'Mammal'}
});

var Primate = Mammal.extend({defaults: {title: 'Primate'}});
var Carnivore = Mammal.extend({defaults: {title: 'Carnivore'}});

Mammal.prototype.subModelTypes['primate'] = Primate;
Mammal.prototype.subModelTypes['carnivore'] = Carnivore;

var Mammals = Backbone.Collection.extend({
model: Mammal
});

var mammals = new Mammals();

mammals.reset([{type: 'primate'}, {type: 'carnivore'}, {type: 'unknown'}]);

strictEqual( mammals.at(0).get('title'), 'Primate', '{type: "primate"} must have {title: "Primate"}' );
strictEqual( mammals.at(1).get('title'), 'Carnivore', '{type: "carnivore"} must have {title: "Carnivore"}' );
strictEqual( mammals.at(2).get('title'), 'Mammal', 'Not existing subModel `type` must have {title: "Mammal"}' );
});

test( "findOrCreate does not modify attributes hash if parse is used, prior to creating new model", function () {
var model = Backbone.RelationalModel.extend({
parse: function( response ) {
Expand Down