我正在使用requirejs並且有一個app.js
,它將拉入framework.js
並對其進行初始化並使用其自己的設置傳入設置和模塊。問題是$('[data-navigation]').navigation();
在navigation
模塊(它是一個jQuery插件)準備好之前觸發。如果我添加一個500毫秒左右的延遲就可以了。來自requirejs模塊的模塊動態加載
require(['jquery-private', 'framework', 'navigation'],
function($, framework, navigation) {
//==========
// Initialize the framework core.
//==========
var core = framework.init({
// Core settings.
namespace: '',
// Initialize modules.
modules: {
navigation: {
openClass: 'open',
},
},
});
//==========
// App logic.
//==========
$('[data-navigation]').navigation();
});
以下是模塊如何初始化。我認爲在腳本繼續運行的同時運行這個require([moduleName], function(module) {}
,這個問題正在發生。
define(['jquery', 'matchmedia'], function($) {
//==========
// Core initialization object.
//==========
var init = function(customOptions) {
//==========
// Logic
//==========
//==========
// Load a module with it's custom options.
//==========
function initModule(module, customOptions, coreObject) {
// Get the previously defined module or the path to load it.
var moduleName = (require.defined(module)) ? module : config.modulesDir + '/' + module;
// Initialize the module.
require([moduleName], function(module) {
var returnObject = module.init(customOptions, coreObject);
// Add to the loaded modules if not already present.
if (settings.modules.indexOf(moduleName) < 0) {
settings.modules.push(moduleName);
settings.m[moduleName] = returnObject;
}
});
return settings.m[moduleName];
}
//==========
// Logic
//==========
//==========
// Build the core object.
//==========
var core = {
// Properties.
// Methods.
}
//==========
// Load the defined modules.
//==========
$.each(config.modules, function(index, value) {
initModule(index, value, core);
});
//==========
// Return the core object.
//==========
return core;
}
//==========
// Return the initialization object.
//==========
return {
init: init
}
});
我已經在這一段時間了。我很確定有一個解決方案,但我似乎無法繞過它。任何指導表示讚賞。
這是導航模塊代碼的一大塊,如果它有幫助。
define(['jquery'], function($) {
//==========
// Module initialization object.
//==========
var init = function(customOptions, core) {
// Ensure customOptions is an object.
var customOptions = typeof customOptions !== 'undefined' ? customOptions : {};
// Get the custom selector or the modules default.
var selector = typeof customOptions.selector !== 'undefined' ? customOptions.selector : '[' + core.space('data-navigation') + ']';
//==========
// Build the jQuery plugin.
//==========
$.fn.navigation = function(options) {
//==========
// Plugin code.
//==========
}
//==========
// Initialize the plugin.
//
// RUNNING THE PLUGIN FROM HERE DOES WORK, BUT I NEED IT TO WORK FROM THE APP.JS TOO!
//
//==========
$(function() {
if ($(selector).length > 0) {
$(selector).navigation(customOptions);
}
});
//==========
// Return object for core.m.[module]
//==========
return {};
}
//==========
// Return the module initialization object.
//==========
return {
init: init
}
});
嘿克利夫,你設置的方式似乎很酷。有什麼方法可以聯繫分享一些想法? –
當然,你可以通過電子郵件給我發送gmail郵件 – CliffAscent