我在我的应用程序中使用 jQuery 和 YUI-3.6.0。我正在使用 YUI 历史模块。我尝试创建一个 YUI 历史记录的包装器,如下所示。 (这保存在“history-wrapper.js”中)
var MyHistory;
(function() {
YUI().use('history', function (Y) {
var history = null;
MyHistory = function() {
history = new Y.History();
};
MyHistory.prototype.save= function() {
history.add({
data : "some data"
});
};
MyHistory.prototype.load= function() {
var data = (history.get("data");
//process data
};
});
})();
我正在使用此包装器和以下代码行
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="yui/yui/yui-min.js"></script>
<script type="text/javascript" src="js/history-wrapper.js"></script>
<script type="text/javascript">
var jQ = jQuery.noConflict();
jQ(document).ready(function() {
var history = new MyHistory();
history.save();
});
</script>
我在两个不同的应用程序中使用相同的代码。
- 在一个应用程序中,由于首先加载了 Wrapper,所以一切正常。
- 在其他应用程序中,它会抛出“MyHistory 未定义”,因为在加载包装器之前调用“jQ(document).ready”函数。
我不知道是什么导致了这种行为。谁能帮我吗?
最佳答案
YUI.use() 中的回调函数“function(Y){... where you Define MyHistory...}”仅在加载依赖项(历史记录)时执行。
然后,当您尝试使用包装器时,您无法保证 MyHistory 已被定义。
解决方案可能是将 JQuery 代码也放入 YUI.use() 中。你可以让YUI加载器加载jquery和history模块,你不再需要包装Histroy插件。
我不确定它是否完全像这样(现在无法检查)。
<script type="text/javascript" src="yui/yui/yui-min.js"></script>
YUI({
modules: {
'jquery': {
path: 'js/jquery.js'
}
}
}).use('history', 'jquery' , function (Y) {
var jQ = Y.jQuery.noConflict(); //or juste jQuery.noConflict();
jQ(document).ready(function() { //it is likely the document will already be ready
var history = new Y.History();
history.save();
});
});
关于javascript - jQuery 和 YUI 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12108018/