我想使用 gridstack 创建一个带有小部件的页面。我的问题是当我“懒惰”加载widgeds时,gridstack不会初始化这个新的dom节点。(它们不可移动)有人可以解释一下我是如何做到这一点的吗?我认为我的问题是 $('.grid-stack').gridstack(options); 正如我在这里看到的,只执行一次:https ://github.com/troolee/gridstack.js/blob/master/src/gridstack.js#L1727
或者是否存在已经意识到“延迟”加载的框架。
索引.html:
<div class="grid-stack" id="widgeds">
<div class="grid-stack-item ui-draggable ui-resizable ui-resizable-autohide ui-resizable-disabled"
data-gs-x="0" data-gs-y="0"
data-gs-width="1" data-gs-height="1">
<div class="grid-stack-item-content ui-draggable-handle">Item 1</div>
</div>
<div class="grid-stack-item"
data-gs-x="1" data-gs-y="0"
data-gs-width="1" data-gs-height="1">
<div class="grid-stack-item-content">Item 2</div>
</div>
</div>
<script type="text/javascript">
var girdstackRefresh = function() {
var options = {
cell_height: 200,
vertical_margin: 10
};
$('.grid-stack').gridstack(options);
}
$(function () {
girdstackRefresh();
loadWidged('/widgeds/chat', function() {
girdstackRefresh();
});
});
//function for lazy loading:
var widgeds = {};
function loadWidged(srcUrl, onloaded) {
$.get(srcUrl+"/meta.wjs", function(meta) {
code = "var foo = function() { return "+meta+"; }\nfoo();";
widged = eval(code);
if(widged.singleton) {
if(widgeds[widged.uid]) {
throw({'message' : 'Error widged can only be loaded once'});
}
}
widged.url = srcUrl;
widged.init.bind(widged);
var ui = $('<div class="grid-stack-item ui-draggable ui-resizable ui-resizable-autohide ui-resizable-disabled" data-gs-x="5" data-gs-y="1" data-gs-width="4" data-gs-height="2">')
$("#widgeds").append(ui);
widged.dom = ui;
widged.init();
widgeds[widged.uid] = widged;
onloaded();
});
}
</script>
Widged 示例:
聊天/meta.wjs:
{
"uid" : "SockIoChat",
"singleton" : true,
"init" : function() {
console.log("debug:");
console.log(this.url);
// $.loadCSS('style.css');
this.dom.load(this.url+"/chat.html")
}
}
聊天/聊天.html:
<div class="grid-stack-item-content ui-draggable-handle">Item 2</div>