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

different types of submodels pointing to the same Model #429

Closed
LiorZ opened this issue Jan 3, 2014 · 2 comments
Closed

different types of submodels pointing to the same Model #429

LiorZ opened this issue Jan 3, 2014 · 2 comments

Comments

@LiorZ
Copy link

LiorZ commented Jan 3, 2014

When using different keys (types) pointing to the same model , only one of the key ended up pointing to the actual model, the other is discarded, and as a result when using type "koko", we get the Mammal Model.

This bug can be avoided when using a different model class for Koko, I presume.

var Animal = Backbone.RelationalModel.extend();

var Mammal = Animal.extend({
    subModelTypes: {
        'primate': 'Primate',
        'carnivore': 'Carnivore',
        'koko':'Carnivore'
    }
});

var AnimalCollection = Backbone.Collection.extend({model:Animal});

window.Primate = Mammal.extend();
window.Carnivore = Mammal.extend();

var MammalCollection = AnimalCollection.extend({
    model: Mammal
});

// Create a collection that contains a 'Primate' and a 'Carnivore'.
var mammals = new MammalCollection([
    { id: 3, species: 'chimp', type: 'primate' },
    { id: 5, species: 'panther', type: 'carnivore' },
    {id: 33, species:'fat_dog', type:'koko' }
]);

var chimp = mammals.get( 3 );

var fat_dog = mammals.get(33);
alert('fat_dog is an animal? ' + ( fat_dog instanceof Animal ) + '\n' +
    'fat_dog is a carnivore? ' + ( fat_dog instanceof Carnivore ) + '\n' +
    'fat_dog is a primate? ' + ( fat_dog instanceof Primate ) );

alert( 'chimp is an animal? ' + ( chimp instanceof Animal ) + '\n' +
    'chimp is a carnivore? ' + ( chimp instanceof Carnivore ) + '\n' +
    'chimp is a primate? ' + ( chimp instanceof Primate ) );
@PaulUithol
Copy link
Owner

Just wondering, why would you have two keys point to the same model?

@LiorZ
Copy link
Author

LiorZ commented Jan 21, 2014

Taking from the above example, If I have a Dog and a Wolf, each should be represented by the Carnivore model but I might want to show them by using different views.

So basically, you might want to reuse the type attribute to assign a different view to a model.

Otherwise, you are forced to create a model (or a dummy model that inherits from the main one) for each such type.

nl0 added a commit to nl0/Backbone-relational that referenced this issue Feb 25, 2014
* upstream/master: (35 commits)
  Upgrade qunit
  Fix subModels not being populated properly on Underscore 1.6.0, due to `_.each` api change.
  Fire change events right away if we're not in a nested scenario. Fixes PaulUithol#427
  Document `store.unregister`
  `store.unregister` now also accepts collections or a model type
  Change `findOrCreate` to pass `parsedAttributes` to `build`, and adjust tests to reflect change.
  Remove unnecessary locking
  Update change log
  Add test for models not being added to store until they get an id
  Change where we listen to `relational:unregister`.
  Only add models with an id to the store. Closes PaulUithol#411
  Fix a bug in the (crude) performance test
  Add a small test for `clear`
  Fix collection return values when setting/removing `[]` and `null`. Ref PaulUithol#419
  Clarify the behavior of `findOrCreate` when it just a receives a scalar value. Ref PaulUithol#399
  Proper return values on collection methods for Backbone 1.1. Closes PaulUithol#419
  Submodels: accommodate multiple 'type' keys for the same submodel. Closes PaulUithol#429
  Hmm, fix spaces/tabs mix in the example
  Update a few version numbers in the docs to 0.8.7
  Backbone-relational 0.8.7
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants