[英]how to refer to a fancytree variable from another function in javascript
我正在嘗試模塊化我的JavaScript。 我對JS並不陌生,但是我的知識不如某些知識先進。 對於精通JS的人來說,這可能是一個簡單的問題。
在下面的代碼中-如何獲取在'handleTreeBrowser'函數中創建的花式樹的句柄,我需要'newTopCategoryForm'的成功函數中的引用嗎? 是否有比僅在更高范圍內創建變量更好的選擇?
我試圖通過JQuery搜索元素,以為它已經被fancytree屬性和方法等所修飾...但是重載函數未定義。
/**
The edit related products page controls
**/
var ManageCategories = function () {
var handleFormButtons = function () {
console.debug('initialising buttons...');
$(document).on('click','.new-topcategory-button', function(event)
{
event.preventDefault ? event.preventDefault() : event.returnValue = false;
console.debug('clicked...');
console.debug($(this).attr('href'));
var refreshURI = $(this).attr('href');
$.get(refreshURI)
.success(function(data,status,xhr){
// need to do something with the data here.
$('#categoryFormContainer').html(data);
RichText.init();
});
})
};
var handleNewTopCategoryForm = function () {
console.debug('initialising top category form');
$(document).on('submit', '#topCategoryForm', function(event)
{
console.debug("submitting the new top category form");
var form = $(this);
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var formData = form.serializeArray();
$(this).find('button').attr('disabled');
event.preventDefault ? event.preventDefault() : event.returnValue = false;
var jqxhr = $.ajax(
{
url: form.attr('action'),
type: 'POST',
data: formData,
headers: {'X-CSRF-TOKEN': token}
}
)
.success(function (data, status, xhr) {
var messageContainer = $('#messageContainer');
var source = $('#ui_message_template').html();
var template = Handlebars.compile(source);
var html = template(data);
messageContainer.html(html);
data.form = form;
// Reload the tree from previous `source` option
$('#tree').reload().done(function(){
alert("reloaded");
});
// we trigger this publish method so the browser can update itself
$.Topic( 'topCategoryAdded' ).publish( data );
})
.fail(function (data, status, xhr) {
var messageContainer = $('#messageContainer');
var source = $('#ui_message_template').html();
var template = Handlebars.compile(source);
var html = template(data);
messageContainer.html(html);
// we have to process the form validation failures here.
});
}
);
}
var handleTreeBrowser = function () {
console.debug('initialising trees.');
$('#tree').fancytree({
source: {
url: '/admin/categories/json/full_category_tree',
cache: false
},
extensions: ["dnd"],
checkbox: false,
selectMode: 2,
activate: function(event, data) {
console.debug(event);
console.debug(data);
console.debug(data.node.key())
},
dnd: {
autoExpandMS: 400,
focusOnClick: true,
preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
preventRecursiveMoves: true, // Prevent dropping nodes on own descendants
dragStart: function(node, data) {
/** This function MUST be defined to enable dragging for the tree.
* Return false to cancel dragging of node.
*/
return true;
},
dragEnter: function(node, data) {
/** data.otherNode may be null for non-fancytree droppables.
* Return false to disallow dropping on node. In this case
* dragOver and dragLeave are not called.
* Return 'over', 'before, or 'after' to force a hitMode.
* Return ['before', 'after'] to restrict available hitModes.
* Any other return value will calc the hitMode from the cursor position.
*/
// Prevent dropping a parent below another parent (only sort
// nodes under the same parent)
/* if(node.parent !== data.otherNode.parent){
return false;
}
// Don't allow dropping *over* a node (would create a child)
return ["before", "after"];
*/
return true;
},
dragDrop: function(node, data) {
/** This function MUST be defined to enable dropping of items on
* the tree.
*/
data.otherNode.moveTo(node, data.hitMode);
}
}
});
}
return {
//main function to initiate the module
init: function () {
handleTreeBrowser();
handleFormButtons();
handleNewTopCategoryForm();
}
};
}();
實際上,以前的答案通常對於javascript通常來說是可以的,所以我將其保留為正常。 但是無論出於何種原因,fancytree樹插件在初始化時都不會返回其自身的實例。 有點奇怪。
因此,針對此特定問題的實際解決方法是使用fancytree api中的此功能。
// from handleform.success
// Reload the tree from previous `source` option
var tree = $("#tree").fancytree('getTree');
tree.reload().done(function(){
alert("reloaded");
});
您可以在函數中聲明fancytree變量,而js閉包將允許您從其他函數訪問它:
var ManageCategories = function () {
var fancytree = null;
// in handleform.success:
if (fancytree !== null)
fancytree....
// in handleTreeBrowser:
fancytree = $("#tree').fancytree(....
只有這兩個功能才能訪問該花式樹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.