javascript - Backbone Model custom URL -
i unsure how call , pass variable model call custom url.
the url trying call is:
http://localhost:8080/sims/resource/class/teacher/{id}
id not class id teacher id.
in main.js unsure how pass parameter model , use in model. how go doing ?
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){ (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; } });
the url
property of model can function concatenates , returns base url , dynamic id
property. this:
baseurl: "http://localhost:8080/sims/resource/class/teacher", url: function() { return this.baseurl + '/' + this.id; },
Comments
Post a Comment