backbone-associations icon indicating copy to clipboard operation
backbone-associations copied to clipboard

Cannot get Backbone.Self to work

Open newtonianb opened this issue 9 years ago • 0 comments

http://jsbin.com/zunoniwuca/1/edit?html,output

My parent_id is never resolved to my object, what am I doing wrong?

I have a simple collection of models and each model has a Backbone.One that points to another model of the same type. The example is pretty straight forward but it just doesn't work and I cannot figure out why. I had the similar setup with backbone relational working but it's just not working with backbone associations.

      var Record = Backbone.AssociatedModel.extend({          
        relations: [
            {
                  type: Backbone.One,
                  key: 'parent_id',
                  relatedModel: Backbone.Self
              }
        ]
      });

      var Records = Backbone.Collection.extend({          
        model: Record
      });


      var record1 = new Record({ id: "AA", name: "RA", parent_id: null });
      var record2 = new Record({ id: "BB", name: "RB", parent_id: "AA" });
      var record3 = new Record({ id: "CC", name: "RC", parent_id: "CC" });

      var records = new Records([ record1, record2, record3]);
      records.at(1).get('parent_id'); // this returns empty object

http://stackoverflow.com/questions/33978481/backbone-associations-backbone-self-is-not-working

newtonianb avatar Nov 29 '15 01:11 newtonianb