这是我创建的自定义jquery插件。我正在使用RequireJs来加载这个插件,但出于某种原因它并没有认识到以下声明:
return this.each(function () {
console.log('b');
});
是否有理由将其调用?
这是完整的插件:
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
var methods = {
init: function (options) {
console.log('a');
return this.each(function () {
console.log('b');
var $element = $(this), // reference to the jQuery version of the current DOM element
element = this; // reference to the actual DOM element
$(this).delegate("li > a", "click", function () {
console.log('click');
// Figure out current list via CSS class
var curList = $element.find("a.current").attr("href").substring(1)
// List moving to
$newList = $(this),
// Figure out ID of new list
listID = $newList.attr("href").substring(1),
// Set outer wrapper height to (static) height of current inner list
$allListWrap = $element.find(".tabCont"),
curListHeight = $allListWrap.height();
$allListWrap.height(curListHeight);
if ((listID != curList) && ($element.find(":animated").length == 0)) {
// Fade out current list
$element.find("#" + curList).fadeOut(base.options.speed, function () {
// Fade in new list on callback
$element.find("#" + listID).fadeIn(base.options.speed);
// Adjust outer wrapper to fit new list snuggly
var newHeight = $element.find("#" + listID).height();
$allListWrap.animate({
height: newHeight
}, function () {
$('.tabCont').css('height', 'auto');
});
// Remove highlighting - Add to just-clicked tab
$element.find(".tabsNav li a").removeClass("current");
$newList.addClass("current");
});
}
return false;
});
});
}
};
$.fn.flazingoTabs = function (method) {
if (methods[method]) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); }
else if (typeof method === "object" || !method) { return methods.init.apply(this, arguments); }
else { $.error("Method " + method + " does not exist on flazingo tabs"); }
}
}));
加载插件(main.clients):
define(['ko', 'mapping', 'flazingotabs', 'spinner'],
function (ko, mapping, tabs, spinner) {
var
initTabs = function () {
$(document).ready(function () {
$("#tabs").flazingoTabs('init');
});
},
run = function () {
spinner.start();
spinner.goByeBye();
initTabs();
});
};
return {
run: run
};
});
这是加载(app.clients.js):
requirejs.config({
"baseUrl": "../Scripts/app/views/administrator/clients",
"paths": {
"app.clients": "app.clients",
"main.clients": "main.clients",
"jquery": "//code.jquery.com/jquery-2.1.0.min",
"spinner": "../../../helpers/spinner",
"ko": "//cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min",
"knockout.postbox": "../../../../lib/knockout-postbox",
"knockout.validation": "../../../../lib/knockout.validation",
'knockout.bindings': "../../../../lib/knockout.bindings",
"mapping": "../../../../lib/knockout.mapping-latest.debug",
"toastr": "../../../../lib/toastr",
"flazingotabs": "../../../../lib/flazingotabs" },
shim: {
"knockout.validation": ["ko"],
"mapping": ["ko"],
"flazingotabs" : ["jquery"]
}
});
require(["ko"], function (ko) {
//manually set the global ko property
window.ko = ko;
//then bring in knockout validation
require(["knockout.validation"], function () {
ko.validation.configure({
insertMessages: false,
decorateElement: true,
errorElementClass: 'error'
});
// Load the main app module to start the app
require(["main.clients"], function (bs) { bs.run(); });
});
});