$.ajax({
type: "POST",
url: "Home.aspx/loadSB",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
for (i = 0; i < msg.d.length; i++) {
$('#sidebar ul').append('<li class="inactive"><a>' + msg.d[i].Title + ' </a></li>');
}
}
});
我通过这个 https://github.com/kjbekkelund/writings/blob/master/published/understanding-backbone.md 和 https://gist.github.com/ryandotsmith/1655019 教程并创建如下主干,但是由于模型没有从 asp.net 获取数据,所以效果不好,哪一部分出了问题?我还是新手
Menu = Backbone.Model.extend({
parse: function (data) { return data; }
});
MenuCol = Backbone.Collection.extend({
model: Menu,
url: "Home.aspx/loadSB",
parse: function (response) { return response.rows; }
});
MenuView = Backbone.View.extend({
initialize: function () {
this.collection.on('add', this.appendMenu, this);
},
appendMenu: function (msg) {
for (i = 0; i < msg.d.length; i++) {
this.$('ul').append('<li class="inactive"><a>' + msg.d[i].Title + ' </a></li>');
}
}
});
var mc = new MenuCol();
//mc.fetch();
//Update1:
new MenuView({ el: $('#sidebar'), collection: mc });
更新 1: 我玩过 fetch,但仍然无法正常工作,
Menu = Backbone.Model.extend({});
MenuCol = Backbone.Collection.extend({
model: Menu,
url: "Home.aspx/loadSB",
});
var test = new MenuCol();
test.fetch();
它没有返回我想要的数据,而是显示我的屏幕具有的界面
如果我使用没有主干的 jquery.ajax,它会将 json 数据返回给我,哪一部分出错了?任何指南都会很棒
更新 2:
好的,我已经更改了“获取”,现在我可以看到传入主干的 json 数据
var test = new MenuCol();
test.fetch({ type: 'POST', contentType: "application/json; charset=UTF-8" });