Backbone Model custom URL

I am unsure how to call and pass a variable into my model to call a custom url.

The url I am trying to call is:

http://localhost:8080/SIMS/resource/class/teacher/{id}

ID is not class ID its teacher ID.

In the main.js I am unsure how to pass an extra parameter into the model and use it in the model. HOw would I go about doing this ?

Model:

window.Class = Backbone.Model.extend({
    urlRoot: "http://localhost:8080/SIMS/resource/class/teacher",
    defaults: {
          "id": null,
        "grade":  "",
        "year": "",
            "Tname": "",
            "Sname": ""
      },

      parse: function(response){
           response.id = response.idClass;
      } ,

      toJSON: function(){
      }
});

window.ClassCollection = Backbone.Collection.extend({
    model: Class,
    url: "http://localhost:8080/SIMS/resource/class/teacher",

        parse: function(response){
          for (var i=0; i<response.length; i++)
           {
               response.id = response.idClass;
           }
           return response ;
        }
});

Main.js

routes: {
 "sidebar/:id": "sidebar"
},

sidebar: function(){
    this.classList = new ClassCollection(); 
    this.classList.fetch({success: function() {
        $('#sidebar-collapse').html( new TeachersidebarView({model: app.classList}).render().el );
        if (callback) callback();
    }});
},

View

window.TeachersidebarView = Backbone.View.extend({
    tagName:'ul',

    initialize:function () {
        this.templateA = _.template(tpl.get(sidebarA));
        this.templateB = _.template(tpl.get(sidebarB));
        this.model.bind("reset", this.render, this);
        var self = this;

        this.model.bind("add", function (Class) {
            $(self.el).append(new TeachersidebarItemView({model:Class}).render().el);
        });
    },

    render:function (eventName) {
         $(this.el).html(this.templateA());
        _.each(this.model.models, function (Class) {
            $(this.el).append(new TeachersidebarItemView({model:Class}).render().el);
        }, this);

        $(this.el).append(this.templateB());
        return this;
    }
});

window.TeachersidebarItemView = Backbone.View.extend({
    tagName:"li",

    initialize:function () {
        this.template = _.template(tpl.get('sidebarC'));
        this.model.bind("change", this.render, this);
        this.model.bind("destroy", this.close, this);
    },

    render:function (eventName) {
        $(this.el).html(this.template(this.model.attributes));
        return this;
    }
});

Answers


The url property of your model can be a function which concatenates and returns the base URL and the dynamic id property. Something like this:

baseURL: "http://localhost:8080/SIMS/resource/class/teacher",
url: function() { return this.baseURL + '/' + this.id; },

If the id you want to pass is not id so you have to provide an idAttribute to your model and Backbone would set it for you :

window.Class = Backbone.Model.extend({
    urlRoot: "http://localhost:8080/SIMS/resource/class/teacher",
    defaults: { ... },
    idAttribute: 'teacherId' // here
    ....

Need Your Help

UIScrollView - tell the difference between setContentOffset and manual scrolling

ios uiscrollview touchesbegan touchesmoved

I have a scrollview, which automatically advances every 3 seconds to the next "page" (page control also used). However, once the user touches the scrollview, I want to stop the auto-advancing.

How to convert TIS-620 string to UTF-8 string in Java?

java string type-conversion

How do I convert TIS-620 (the extended ASCII Thai Character code page) string to UTF-8 string in Java?