I am new to the backbone. I tried to do ajax call in the below manner. And I am not sure how to handle the data from my get request? Here is the code:
我是骨干的新手。我尝试以下面的方式进行ajax调用。我不知道如何处理我的get请求中的数据?这是代码:
//Interacting with the server
//与服务器交互
var UserModel = Backbone.Model.extend({
url:'http://api.geonames.org/astergdemJSON?formatted=true&lat=50.01&lng=10.2&username=demo&style=full'
});
var MyView = Backbone.View.extend({
initialize: function() {
this.model = new UserModel();
this.model.fetch();
},
render: function() {
alert('do awesome stuff here');
}
});
1 个解决方案
#1
10
When you make fetch()
, Backbone will send an GET ajax request to the endpoint you specifiend on url
.
当你进行fetch()时,Backbone会向你在url上指定的端点发送一个GET ajax请求。
When you call .fetch
, the model will trigger a request
event, and when you receive it, parse it and set it to the model, the model will trigger a sync
event.
当您调用.fetch时,模型将触发请求事件,当您收到它时,解析它并将其设置为模型,模型将触发同步事件。
If you want to change any data before setting it to the model, override the method parse
on you model. It receives the raw data from the GET request and must return what is going to be set to your model.
如果要在将任何数据设置为模型之前更改任何数据,请覆盖模型上的方法解析。它从GET请求接收原始数据,并且必须返回将要设置到模型的内容。
If you want extra success and error callbacks, pass them in you your fecth({ success: ..., error: ... })
. Success callback is triggered after setting and parsing the values to the model and before sync event. Error callback is triggered before error event. Look the documentation for more details. It is not necessary to pass any of those callbacks.
如果你想获得额外的成功和错误回调,请将它们传递给你的fecth({success:...,error:...})。在将值设置和解析到模型之后以及同步事件之前触发成功回调。错误事件之前触发错误回调。查看文档以获取更多详细信息。没有必要通过任何回调。
So, it would be something like:
所以,它会是这样的:
var MyView = Backbone.View.extend({
initialize: function() {
this.model = new UserModel();
this.listenTo(this.model, 'sync', this.render);
this.model.fetch();
},
render: function() {
alert('My model loaded: ' + this.model.toJSON());
}
});
And, that's it. Backbone makes almost everything for you.
而且,就是这样。 Backbone几乎可以为您提供一切。
Hope I've helped.
希望我能帮到你。
#1
10
When you make fetch()
, Backbone will send an GET ajax request to the endpoint you specifiend on url
.
当你进行fetch()时,Backbone会向你在url上指定的端点发送一个GET ajax请求。
When you call .fetch
, the model will trigger a request
event, and when you receive it, parse it and set it to the model, the model will trigger a sync
event.
当您调用.fetch时,模型将触发请求事件,当您收到它时,解析它并将其设置为模型,模型将触发同步事件。
If you want to change any data before setting it to the model, override the method parse
on you model. It receives the raw data from the GET request and must return what is going to be set to your model.
如果要在将任何数据设置为模型之前更改任何数据,请覆盖模型上的方法解析。它从GET请求接收原始数据,并且必须返回将要设置到模型的内容。
If you want extra success and error callbacks, pass them in you your fecth({ success: ..., error: ... })
. Success callback is triggered after setting and parsing the values to the model and before sync event. Error callback is triggered before error event. Look the documentation for more details. It is not necessary to pass any of those callbacks.
如果你想获得额外的成功和错误回调,请将它们传递给你的fecth({success:...,error:...})。在将值设置和解析到模型之后以及同步事件之前触发成功回调。错误事件之前触发错误回调。查看文档以获取更多详细信息。没有必要通过任何回调。
So, it would be something like:
所以,它会是这样的:
var MyView = Backbone.View.extend({
initialize: function() {
this.model = new UserModel();
this.listenTo(this.model, 'sync', this.render);
this.model.fetch();
},
render: function() {
alert('My model loaded: ' + this.model.toJSON());
}
});
And, that's it. Backbone makes almost everything for you.
而且,就是这样。 Backbone几乎可以为您提供一切。
Hope I've helped.
希望我能帮到你。