我对 Backbone 很陌生,我一直在尝试用它创建自动完成功能,由 ASMX 网络服务提供。我似乎遇到的问题是,当我的 web 服务以 JSON 格式返回时(在与它进行了一场痛苦的战斗之后),它将响应包装在一个“d”(数据集)中。如何让视图理解这一点并获取正确的数据?
这是我的代码:-
var Airline = Backbone.Model.extend({
initialize: function () {},
defaults: {
name: 'Airline Name',
rating: 50,
icon: '/blank.png'
}
});
var AirlineCollection = Backbone.Collection.extend({
model: Airline,
contentType: "application/json",
url: '/ControlTower/public/runway.asmx/all-airlines',
parse: function (response) {
return response;
}
});
var SelectionView = Backbone.View.extend({
el : $('#airline'),
render: function() {
$(this.el).html("You Selected : " + this.model.get('AirlineName'));
return this;
},
});
var airlines = new AirlineCollection();
airlines.fetch({async: false, contentType: "application/json" });
var airlineNames = airlines.pluck("AirlineName");
$("#airline").autocomplete({
source : airlineNames,
minLength : 1,
select: function(event, ui){
var selectedModel = airlines.where({name: ui.item.value})[0];
var view = new SelectionView({model: selectedModel});
view.render();
}
});
谁能看到我做错了什么?我已经坐在这里太久了!
帮助表示赞赏;)