gpt4 book ai didi

javascript - YUI 库 - 保持对对象的全局引用的最佳方式?

转载 作者:行者123 更新时间:2023-11-29 20:11:42 26 4
gpt4 key购买 nike

我正在尝试使用 yahoo ui 历史库。我没有找到避免用 Y.use 包装我的所有函数内容以便我可以访问历史对象的好方法。我尝试在 use() 命令之外全局声明它,但这似乎不起作用。如果您查看我的 showDashboard() 和 showReport1() 方法,您会发现我正在包装内容,对于使用历史记录的每个函数都必须这样做似乎是多余的。有一个更好的方法吗?

我见过的所有 yahoo 示例都根本没有 se 函数,而是将整个示例保存在一个单一的 use 方法中。

 <div>
<a href="#" onclick="showDashboard(); return false;">Dashboard</a> |
<a href="#" onclick="showReport1(); return false;">Report 1</a>
</div>
<script type="text/javascript">
// Global reference to Yahoo UI object
var Y = YUI();

function showDashboard() {
Y.use('*', function (Y) {
var history = new Y.HistoryHash();
history.addValue("report", "dashboard");
});
}

function showReport1() {
Y.use('*', function (Y) {
var history = new Y.HistoryHash();
history.addValue('report', "report1");
//var x = { 'report': 'report1', 'date': '11/12/2012' };
//history.addValue("report", x);

});
}

Y.use('history', 'tabview', function (Y) {
var history = new Y.HistoryHash();
var tabview = new Y.TabView({ srcNode: '#demo' });

// Render the TabView widget to turn the static markup into an
// interactive TabView.
tabview.render();

// Set the selected report to the bookmarked history state, or to
// the first report if there's no bookmarked state.
tabview.selectChild(history.get('report') || 0);

// Store a new history state when the user selects a report.
tabview.after('selectionChange', function (e) {
// If the new tab index is greater than 0, set the "tab"
// state value to the index. Otherwise, remove the "tab"
// state value by setting it to null (this reverts to the
// default state of selecting the first tab).
history.addValue('report', e.newVal.get('index') || 0);
});

// Listen for history changes from back/forward navigation or
// URL changes, and update the report selection when necessary.
Y.on('history:change', function (e) {
// Ignore changes we make ourselves, since we don't need
// to update the selection state for those. We're only
// interested in outside changes, such as the ones generated
// when the user clicks the browser's back or forward buttons.
if (e.src === Y.HistoryHash.SRC_HASH) {
if (e.changed.report) {
// The new state contains a different report selection, so
// change the selected report.
tabview.selectChild(e.changed.report.newVal);
} else if (e.removed.report) {
// The report selection was removed in the new state, so
// select the first report by default.
tabview.selectChild(0);
}
}

if (e.changed.report) {
alert("New value: " + e.changed.report.newVal);
alert("Old value: " + e.changed.report.prevVal);
}
});
});
</script>
</form>
</body>
</html>

最佳答案

不是在点击时使用普通函数,而是使用 YUI 附加处理程序。如果您可以更改 HTML 代码 - 将 id 或 class 添加到链接,例如

<a id="btnShowDashboard" href="#">Dashboard</a>

然后在您的 use() 中为按钮添加点击处理程序

Y.use('history', 'tabview', 'node', 'event', function (Y) {

var bntShowDashboard = Y.one('#btnShowDashboard');
if (bntShowDashboard) {
bntShowDashboard.on('click', function(e) {
e.preventDefault();
var history = new Y.HistoryHash();
history.addValue("report", "dashboard");
});
}

...
})

这样你就可以确定在执行“历史记录”的那一刻被加载了。但是有一个缺点 - 在加载 YUI 模块之前,如果您单击链接,则什么也不会发生。

关于javascript - YUI 库 - 保持对对象的全局引用的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com