0

I am using backbone with Node and Express. I have my restful api set up to return my model and collection data. The api works fine. But I'm having trouble binding a route to one of my api paths.

I have a company model and collection so that when go to the routes you get the restful api data for that route:

http://localhost:3000/employees you get the data for restful api path api/employees http://localhost:3000/employees/1 you get the data for restful api path api/employees/1

I also have a category model and collection to do the same:

http://localhost:3000/categories you get the data for restful api path api/categories

but the following does not work: http://localhost:3000/categories/Auto you don't get the data for restful api path api/cateogries/Auto The restful api works and returns the right data, but the collection I get in my app returns the same data as the category collection called with the path api/cateogries. Almost like the path gets ignored.

Typically you have a collection and then you provide a id attribute to get a model that belongs to that collection. But what if you want a collection whose id attribute returns another collection? For example, you get a list of categories and then when you select a category you get a list of all the companies in that category? What is the right way to do this in backbone?

mo_maat
  • 2,110
  • 12
  • 44
  • 72
  • Can you post some code meanwhile check out if [this](http://backbonerelational.org/) or [this](https://github.com/afeld/backbone-nested) is of any help to you – nikhil mehta Jul 29 '16 at 08:49
  • Thanks. I actually figured it out. I was returning a collection and needed to adust the url of my collection to point it to the correct path in my restful api. So in my collection I added a url function that goes to a different route if a parameter is passed into the constructore: – mo_maat Jul 30 '16 at 01:45

1 Answers1

0

Thanks. I actually figured it out. I was returning a collection and needed to adust the url of my collection to point it to the correct path in my restful api. So in my collection I added a url function that goes to a different route if a parameter is passed into the constructor:

url:function(){
        if(this.category1){
            return "api/categories/"+this.category1;
        }
        return "api/categories";
      },

    initialize:function(opts){
        this.category1=opts && opts.category1;
    }
mo_maat
  • 2,110
  • 12
  • 44
  • 72