我无法为以下问题找到一个优雅的解决方案。
我创建了一个小型的jQuery移动小部件,它丰富了输入字段中与金额和货币相关的内容。为了不重新发明轮子,我加入了autoNumeric.js插件。例如,在_create方法中,我正在执行以下操作。
/**
* autoNumeric.js required (https://github.com/BobKnothe/autoNumeric)
*/
(function($, window, document, undefined) {
"use strict";
$.widget("mobile.amountinput", $.mobile.textinput, {
// other code here
_create : function() {
// other code here
$element.autoNumeric('init');
$element.focus(function(event) {
var o = { aDec: ',', aSep : '.', pSign: 's', aSign: '' };
$element.autoNumeric('update', o);
});
$element.blur(function(event) {
var o = { aDec: ',', aSep : '.', pSign: 's', aSign: ' €' };
$element.autoNumeric('update', o);
});
}
});
$.mobile.document.bind("pagecreate create", function(e) {
$.mobile.input.prototype.enhanceWithin(e.target, true);
});
}(jQuery, window, document));autoNumeric.js是通过require.js加载的,但我希望使依赖关系显式化(比如在require.js模块定义中)。通过这种方式,我可以确定autoNumeric.js的存在和导入是正确的。
有办法做到这一点吗?
发布于 2013-12-05 09:50:37
下面这样的东西应该能起作用。将传递给define的依赖项列表中的define依赖项替换为适用于您的情况。我不知道为什么在参数列表中需要undefined,但我将它保存在那里。
(function (factory) {
// If in an AMD environment, define() our module, else use the
// jQuery global.
'use strict';
if (typeof define === 'function' && define.amd)
define(['jquery', 'autonumeric'], function ($) {
factory($, window, document);
});
else
factory($, window, document);
}(function($, window, document, undefined) {
"use strict";
$.widget("mobile.amountinput", $.mobile.textinput, {
// other code here
_create : function() {
// other code here
$element.autoNumeric('init');
$element.focus(function(event) {
var o = { aDec: ',', aSep : '.', pSign: 's', aSign: '' };
$element.autoNumeric('update', o);
});
$element.blur(function(event) {
var o = { aDec: ',', aSep : '.', pSign: 's', aSign: ' €' };
$element.autoNumeric('update', o);
});
}
});
$.mobile.document.bind("pagecreate create", function(e) {
$.mobile.input.prototype.enhanceWithin(e.target, true);
});
});https://stackoverflow.com/questions/20396237
复制相似问题