简体   繁体   English

Backbone Model自定义URL

[英]Backbone Model custom URL

I am unsure how to call and pass a variable into my model to call a custom url. 我不确定如何调用并将变量传递给我的模型来调用自定义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. ID不是班级ID,而是教师ID。

In the main.js I am unsure how to pass an extra parameter into the model and use it in the model. 在main.js中,我不确定如何将额外的参数传递给模型并在模型中使用它。 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 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;
    }
});

The url property of your model can be a function which concatenates and returns the base URL and the dynamic id property. 模型的url属性可以是连接并返回基本URL和动态id属性的函数。 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 : 如果您要传递的ID不是id那么您必须为您的模型提供idAttribute ,Backbone会为您设置它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM