1

I'm having this slight problem with Backbone-Relational when I try to create sub models inside the marionette module. Following backbone-relational documentation I have:

            Mammal = Backbone.RelationalModel.extend({
                subModelTypes: {
                    'primate': 'Primate',
                    'carnivore': 'Carnivore'
                }
            });

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

            MammalCollection = Backbone.Collection.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' }
            ]);

            var chimp = mammals.get( 3 );

            alert( 'chimp is a mammal? ' + ( chimp instanceof Mammal ) + '\n' +
                'chimp is a carnivore? ' + ( chimp instanceof Carnivore ) + '\n' +
                'chimp is a primate? ' + ( chimp instanceof Primate ) );

This works perfectly fine in jsfiddle as well as on my end. chimp is an instance of Mammal and a Primate. My problem starts when this code gets run inside Marionette module like so:

define(
    [
        "app",
        "backbone-relational",

    ],
    function(ManagerApp){

        ManagerApp.module("Entities", function (Entities, ManagerApp, Backbone, Marionette, $, _) {

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

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

            var MammalCollection = Backbone.Collection.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' }
            ]);

            var chimp = mammals.get( 3 );

            alert( 'chimp is an animal? ' + ( chimp instanceof Mammal ) + '\n' +
                'chimp is a carnivore? ' + ( chimp instanceof Carnivore ) + '\n' +
                'chimp is a primate? ' + ( chimp instanceof Primate ) );

        });

    })

In this case, I'm getting true for chimp instanceof Mammal, but false for Primate and Carnivore. I realize that all my models should be attached to Entities but that did not work either and I wanted to test the simplest scenario possible.

Why are proper submodels not created ?

MikeWu
  • 3,042
  • 2
  • 19
  • 27
  • Try looking at http://stackoverflow.com/questions/13276388/backbone-relational-submodels-with-requirejs – baohouse Apr 26 '15 at 17:42

0 Answers0