0
    $.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.mdhttps://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" });
4

1 回答 1

0

好的,我终于解决了这个问题,

define(['jquery', 'underscore', 'backbone'], function () {
Menu = Backbone.Model.extend({});

MenuCol = Backbone.Collection.extend({
    model: Menu,
    url: "Home.aspx/loadSB",
});

MenuView = Backbone.View.extend({

    initialize: function () {
        this.collection.on('add', _.bind(this.AppendMenu, this));
    },
    AppendMenu: function (msg) {
        var feed = JSON.parse(JSON.stringify(msg));
        for (var i = 0; i < feed.d.length; i++) {
            this.$('ul').append('<li class="inactive"><a>' + feed.d[i].Title + ' </a></li>');
        }
    }
});

var test = new MenuCol();
test.fetch({ type: 'POST', contentType: "application/json; charset=UTF-8" });
new MenuView({ el: $('#sidebar'), collection: test })

});

通过将获取类型更改为“POST”和 contentType:“application/json;charset=UTF-8”,最后我设法用我想要的数据填充 web

于 2013-06-10T02:56:53.103 回答