1

I'm quite new using Backbone and now I have found this new issue.

I use the route "jobprofile" to create a view which fetch the data from urlRoot= "job" (doing job/id using a default id) BUT if I add the :id to the route as "jobprofile/:id" which I need to type in the browser to be able to get the job.id view, then it stops to work and the url of the model change to: ".../jobprofile/job/id" which (obviously) give me 404 error.

Hope is clear. Thanks!

CODE:

I have a router.js

routes: {
        ...
        "jobprofile/:id":   "view", //without /:id works!
    },

view:function(id){
        console.log("view");

        this.job = new Job();
        this.job.setId(id); //This is set correctly
        this.jobProfileView = new JobProfileView({
            model: this.job,
            el: $('.tab-content')
        });
    },

(View)JobProfileView.js:

...
initialize: function(){
        var that = this;
        this.model.fetch().done(function(){
            console.log("fetch done!");
            that.render();
        });
    },
...

(Model)Job.js:

urlRoot: 'job',

initialize: function () {
},

setId: function (job_id) {
    this.set('id', job_id);
},

UPDATED:

Ok. So it looks that I "fix" the problem adding this.navigate('/jobprofile'); to the method view in router.js. I guess that the /:id which causes the problem is deleted from the route (actually when you see the browser its not there anymore) but I still keep the id in the method.

In any case, this is a really bad solution because when I try to go back it creates a bucle and it goes to jobprofile/id and navigate again to jobprofile. So if anyone has an idea it would be great...

juangv
  • 111
  • 11
  • Which piece/s of this URL is/are causing the problem: ".../jobprofile/job/id"? – pdoherty926 Nov 15 '15 at 01:29
  • I ask for a Job doing: 'job/1' to get a json response. To see the Job view in the browser I write:url/jobprofile/1. My root in history.start is "/url". So, when I create the route jobprofile (without :id) and I write in my browser 'url/jobprofile' backbone make the request to the url = '/url/job/1' (id=1 as default) to get the model and it's ok!. BUT if I decide to use another id and I add 'jobprofile/:id' to my routes, then (I guess) the start.root changes to 'url/jobprofile' and if I write in the browser 'url/jobprofile/2' the request to get the model is done to 'url/jobprofile/job/2'. – juangv Nov 15 '15 at 07:24

1 Answers1

0

Finally I understood what the problem was...

Basically there is a difference when in the url or urlRoot are set in the model. Thus, these two options appear:

  1. url:'/foo'. In this case it will not take the base url. example: www.web.com/foo

  2. url:'foo'. In this case, it will take the base url example: www.web.com/api/foo

My case is as follow:

(Model)Job.js:

urlRoot: '/job',

initialize: function () {
},

setId: function (job_id) {
    this.set('id', job_id);
},
juangv
  • 111
  • 11