0

I am just starting to learn Backbone.js. How do i create a Backbone model for the following use case

  • States:[0-n]
    • name
    • counties:[0-n]
      • name
      • cities:[0-n]
        • name:
        • parks[0-n]
          • name:

Any help will is greatly appreciated. this is what i tries so far

window.Team = Backbone.Model.extend({
        initialize: function(){
          this.id = this.get('id');
        }
      });
      window.Teams = Backbone.Collection.extend({model:Team});

      window.Ward = Backbone.Model.extend({
        initialize: function(){
          this.name = this.get('name');
          this.code = this.get('code');
          this.teams = new Teams(this.get('teams'));
        }
      });

      window.Wards = Backbone.Collection.extend({model:Ward});

      window.LGA = Backbone.Model.extend({
        initialize: function(){
          this.name = this.get('name');
          this.wards = new Wards(this.get('wards'));
        }
      });

      window.LGAs = Backbone.Collection.extend({model:LGA});

      window.State = Backbone.Model.extend({
        initialize: function(){
          this.name = this.get('name');
          this.lgas = new Wards(this.get('lgas'));
        }
      });

      window.States = Backbone.Collection.extend({model:State,

        initialize: function(){
          _.bindAll(this, 'fetch_success');
          this.bind('change', this.fetch_success);
        },

        url: function(){
            return "data.json"
        },

        fetch_success:function(){
            var data = Backbone.Syphon.serialize(someViewWithAForm);
            var model = new Backbone.Model(data);
        }
      });

Thanks

2
  • Show us whathaveyoutried.com , please. This shows what you have until now and that you are not after code for free guys. Commented Jun 20, 2012 at 16:38
  • Updated with the code i tried Commented Jun 20, 2012 at 17:54

1 Answer 1

2

Depending on how you build/fetch your models, you may be interested in Backbone-relational, which lets you define relationships between models.

From the docs:

Person = Backbone.RelationalModel.extend({
    relations: [
        {
            type: 'HasMany',
            key: 'jobs',
            relatedModel: 'Job',
            reverseRelation: {
                key: 'person'
            }
        }
    ]
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.