These are my initial days of working on BackBone.js. I am actually stuck with the router part as i am getting an error in the console "Error: a url property or function must be specified". This is my scenario; I have a click function which dynamically forms a REST URL, and fetch the data from server accordingly.
Here is the click function code
function onUserClick(obj){
var userID=$(obj).attr('id');
window.location.href="#"+userID+"/";
var userRouter = new UserRouter;
}
And I have the following code in the router
var userModel;
var UserRouter = Backbone.Router.extend({
routes:{
":userid/":"getUsers"
},
getUsers:function(userid){
var url="http://myserver.com/users/"+userid;
userModel = new UserModel({urlRoot:url});
var userView = new UserView({el:$(#"container")});
}
});
var UserModel = Backbobe.Model.extend({});
var UserView = Backbone.View.extend({
model:userModel,
initialize:function(){
_.templateSettings = {
interpolate: /\{\{\=(.+?)\}\}/g,
evaluate: /\{\{(.+?)\}\}/g
};
this.model.fetch();
this.model.bind("change",this.render,this);
},
render:function(){
var data=this.model.toJSON();
var userTemplate=_.template($("#userTemplate").text());
var htmlData=userTemplate({
"userData":data
});
$(this.el).html(htmlData);
}
});
Can someone help me to figure out the issue here? I know i have done something wrong here and seeks an expert advice on this. A woking sample for this scenario is much appreciated.