首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为jQuery (移动)小部件/插件添加显式依赖项

为jQuery (移动)小部件/插件添加显式依赖项
EN

Stack Overflow用户
提问于 2013-12-05 09:32:40
回答 1查看 406关注 0票数 1

我无法为以下问题找到一个优雅的解决方案。

我创建了一个小型的jQuery移动小部件,它丰富了输入字段中与金额和货币相关的内容。为了不重新发明轮子,我加入了autoNumeric.js插件。例如,在_create方法中,我正在执行以下操作。

代码语言:javascript
复制
/**
 * 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的存在和导入是正确的。

有办法做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-05 09:50:37

下面这样的东西应该能起作用。将传递给define的依赖项列表中的define依赖项替换为适用于您的情况。我不知道为什么在参数列表中需要undefined,但我将它保存在那里。

代码语言:javascript
复制
(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);
    });

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20396237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档