Chito Cheng
Chito Cheng

Reputation: 550

backbone model cannot fetch any data

Here're my simplified codes

var PlayerModel = Backbone.Model.extend();

var PlayerProfileView = Backbone.View.extend({
model : new PlayerModel({url: "http://mysite.com/api/getpalyer.php"}),
render: function(){
this.model.fetch();
}
});

the system keeps giving me this error message

Uncaught Error: A "url" property or function must be specified

I totally have no idea what's wrong with my code.

Upvotes: 1

Views: 120

Answers (2)

Loamhoof
Loamhoof

Reputation: 8293

Here's the Backbone#Model's constructor:

var Model = Backbone.Model = function(attributes, options) {

So as you see, the options should be given as the second argument. Here you're passing your url as an attribute (try this.get('url') to verify that).

Change it to:

model : new PlayerModel(null, {url: "http://mysite.com/api/getpalyer.php"});

Another thing:

Also, declaring a new object in the class definition (new PlayerModel({url: "http://mysite.com/api/getpalyer.php"}),) will result in having a single instance of this object shared by all your objects (ie any PlayerProfileView will share a single instance of PlayerModel). The reason behind it is that it's evaluated when the class is created and put in the prototype of your class.

Upvotes: 3

Tan Nguyen
Tan Nguyen

Reputation: 3354

I think you should put url in the definition of PlayerModel instead of passing it when you call new. If you still want to pass it in new, you need to specify it as the second param (as Loamhoof pointed out)

var PlayerModel = Backbone.Model.extend({
  url: "http://mysite.com/api/getpalyer.php"
});

Upvotes: 0

Related Questions