Backbone 从 Json 获取数据
Backbone fetch Data from Json
我是 Backbone 的新手。我正在尝试从 backbone.But 中的 json
获取数据,但未获取数据。尝试获取 data.Here 我的代码时出现错误功能:
var Item = Backbone.Model.extend();
var List = Backbone.Collection.extend({
model: Item,
url: "form.json",
parse: function(response) {
return response.results;
},
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: false
}, options);
return $.ajax(params);
}
});
var ListView = Backbone.View.extend({
el: $('.deep-div'),
events: {
'click button#add': 'getPost'
},
initialize: function() {
_.bindAll(this, 'getPost');
this.collection = new List();
this.getPost();
},
getPost: function() {
var that = this;
this.collection.fetch({
success: function() {
console.log(that.collection.toJSON());
},
error: function() {
console.log('Failed to fetch!');
}
});
}
});
var listView = new ListView();
我的json
是
{
"status": "SUCCESS",
"statusMessage": "",
"payload": [
{
"type": "text",
"label": "Name",
"currentValue": " ",
"isRequired": "true"
},
{
"type": "Date",
"label": "DOB",
"currentValue": " ",
"isRequired": "true"
}
]
}
不要在不知道自己在做什么的情况下重写 sync
方法。
如果 JSON 文件的路径是正确的,并且您通过 HTTP 服务器为应用程序提供服务并且不存在文件访问权限问题,则以下内容将有效。
var List = Backbone.Collection.extend({
model: Item,
url: "form.json",
parse: function(response) {
return response.payload;
}
});
我是 Backbone 的新手。我正在尝试从 backbone.But 中的 json
获取数据,但未获取数据。尝试获取 data.Here 我的代码时出现错误功能:
var Item = Backbone.Model.extend();
var List = Backbone.Collection.extend({
model: Item,
url: "form.json",
parse: function(response) {
return response.results;
},
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: false
}, options);
return $.ajax(params);
}
});
var ListView = Backbone.View.extend({
el: $('.deep-div'),
events: {
'click button#add': 'getPost'
},
initialize: function() {
_.bindAll(this, 'getPost');
this.collection = new List();
this.getPost();
},
getPost: function() {
var that = this;
this.collection.fetch({
success: function() {
console.log(that.collection.toJSON());
},
error: function() {
console.log('Failed to fetch!');
}
});
}
});
var listView = new ListView();
我的json
是
{
"status": "SUCCESS",
"statusMessage": "",
"payload": [
{
"type": "text",
"label": "Name",
"currentValue": " ",
"isRequired": "true"
},
{
"type": "Date",
"label": "DOB",
"currentValue": " ",
"isRequired": "true"
}
]
}
不要在不知道自己在做什么的情况下重写 sync
方法。
如果 JSON 文件的路径是正确的,并且您通过 HTTP 服务器为应用程序提供服务并且不存在文件访问权限问题,则以下内容将有效。
var List = Backbone.Collection.extend({
model: Item,
url: "form.json",
parse: function(response) {
return response.payload;
}
});