TopicsListModel
次のようなプロパティを持つサーバー側クラスがあります。
public class TopicsListModel
{
public TopicsListModel()
{
ChildTopics = new HashSet<TopicsListModel>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TopicsListModel> ChildTopics { get; set; }
}
リストを返す関数があり、
public JsonResult SearchTopic(String topic)
{
var topics = LandingManager.SearchTopics(topic);
//return Json(topics);
return new JsonResult { Data = topic, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
これをバックボーン モデルとコレクションに追加する必要があります。私は Backbone の初心者で、ご想像のとおり苦労しています。どんな助けでも大歓迎です。
次のようなデータを保存するモデルを構築したい:
{ name: "Model1", id: 1, submodels: [{ name: "Submodel1", id: 2 }, { name: "Submodel1", id: 2 }] }
そのような基本的なコレクション、共有したデータを返す ASP.NET MVC コード部分の設定に問題があります。バックボーンで行ったことを共有します。
TestBB = function () {
if (!window.console) window.console = { log: function () { } };
var treeModel = Backbone.Model.extend({});
var treeSubModel = Backbone.Model.extend({});
var treeCollection = Backbone.Collection.extend({
model: treeSubModel,
initialize: function () {
console.log('Collection Initialized');
}
});
var treeView = Backbone.View.extend({
el: $('#tree-view'),
initialize: function () {
// this.collection.bind("render", this.render, this);
// this.collection.bind("addAll", this.addAll, this);
// this.collection.bind("addOne", this.addOne, this);
_.bindAll(this);
},
render: function () {
console.log("render");
console.log(this.collection.length);
$(this.el).html(this.template());
this.addAll();
var template = _.template($("#template").html(), {});
this.el.html(template);
},
addAll: function () {
console.log("addAll");
this.collection.each(this.addOne);
},
addOne: function (model) {
console.log("addOne");
view = new treeView({ model: model });
$("ul", this.el).append(view.render());
},
events: { "click #btnFind": "doFind" },
doFind: function (event) { alert('event fired'); }
});
return {
TreeView: treeView
};} (Backbone);
提案してください。