最近、Backbone.js に大きく依存するアプリケーションを継承しました。私のアプリケーションは Backbone.sync() 関数をオーバーライドして Qt で動作します (アプリケーションがデスクトップ アプリケーションに埋め込まれたブラウザーで AJAX 要求を実行できるようにします)。そのため、AJAX にはできるだけ Backbone を使用することをお勧めします。
ここでjQuery Treeview プラグインを使用し、Backbone を使用してデータ用の API とやり取りしたいと思います。ノードを非同期的にロードするために、これはtoggle() をオーバーライドする追加のプラグインを使用し、$.ajax を使用して新しいノード データを要求します。
このプラグインで Backbone を使用することに意味はありますか? バックボーンを直接使用する代替の「非同期」プラグインを作成する必要があると思いますか?
これが私がこれまでに持っているものです:
;(function($) {
var proxied = $.fn.treeview;
$.fn.treeview = function(settings) {
// if (!settings.url) {
// return proxied.apply(this, arguments);
// }
var container = this;
var TreeNodeCollection = Backbone.Collection.extend({
url: '/api/subfolder_list',
tagName: 'ul',
initialize: function() {
},
parse: function(response) {
container.empty();
$.each(response, this.createNode, [container]);
//$(container).treeview({add: container});
},
createNode: function(parent) {
var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
if (this.classes) {
current.children("span").addClass(this.classes);
}
if (this.expanded) {
current.addClass("open");
}
if (this.hasChildren || this.children && this.children.length) {
var branch = $("<ul/>").appendTo(current);
if (this.hasChildren) {
current.addClass("hasChildren");
if (typeof branch.collection == 'undefined') {
branch.collection = new TreeNodeCollection();
}
branch.collection.createNode.call({
classes: "placeholder",
text: " ",
children:[]
}, branch);
}
if (this.children && this.children.length) {
if (typeof branch.collection == 'undefined') {
branch.collection = new TreeNodeCollection();
}
$.each(this.children, parent.collection.createNode, [branch])
}
}
$(parent).treeview({add: container});
}
});
container.collection = new TreeNodeCollection();
if (!container.children().size()) {
container.collection.fetch();
}
var userToggle = settings.toggle;
return proxied.call(this, $.extend({}, settings, {
collapsed: true,
toggle: function() {
var $this = $(this);
if ($this.hasClass("hasChildren")) {
var childList = $this.removeClass("hasChildren").find("ul");
//load(settings, this.id, childList, container);
container.collection.fetch();
}
if (userToggle) {
userToggle.apply(this, arguments);
}
}
}));
};
})(jQuery);