0

这是我在 require.config.shim 中的内容(在 main.js 中):

'jquery.tabs':{
    deps:['jquery'],
    exports: '$'
},

以下是我模块中的相关部分:

define(['jquery','underscore','backbone','tools','jquery.tabs'], function($,_,Backbone,Tools){

    //SNIP 

    myView = Backbone.View.extend({                
        //SNIP
        render:     function(){
            //SNIP
            var el = tmp.find(".tabholder")
            console.log(el); // not empty
            console.log($.fn.createTabHolder); //not empty

            el.createTabHolder(); //TypeError: el.createTabHolder is not a function
            //el.createPopup();  //different plugin, same error here
            //el.hide();         // this works just fine
            //SNIP
        },
        //SNIP

    });
    //SNIP
}); 

当我使用 Chrome 或从 localhost 运行它时,它工作得很好,但是当我使用 Firefox (22.0) 从服务器运行它时,我得到“TypeError: el.createTabHolder is not a function”。

这是插件代码以防万一,在我切换到使用requirejs之前它曾经工作得很好:

(function (jQuery){
    jQuery.fn.createTabHolder = function (){
        this.html("");
        var tbar = $("<div/>",{
            class:"tabbar padfix noselect"
        });

        tbar.appendTo(this);

        var tholder = $("<div/>",{
            class:"tabcontainer"
        });

        tholder.appendTo(this);


    };

    jQuery.fn.addTab = function(title,data,index, constructor,model,obj){
        var self=this;
        var ts = $("<span/>",{
            class:"tabselector",
            html:title,

        });

        var tab = $("<div/>",{
            class:"tabselector_tab"

        });
        if(data.jQuery)
            tab.append(data);
        else
            tab.html(data);
        tab.appendTo(this.find(".tabcontainer"));
        if(constructor)
            ts.one("click",{element:tab,model:model,obj:obj},constructor);

        ts.on("click",function(){
            self.find(".selectedtab").removeClass("selectedtab");
            tab.addClass("selectedtab");
            self.find(".activetabselector").removeClass("activetabselector");
            ts.addClass("activetabselector");
        });
        if(this.find(".activetabselector").length==0)
            ts.trigger("click");

        ts.appendTo(this.find(".tabbar"));
    }


    return jQuery;
})(jQuery);

不知道发生了什么,除此之外无法真正提供任何东西。

4

2 回答 2

1

也许那里有不同版本的jquery。尝试这个:

define(['jquery'], function (jQuery){
    jQuery.fn.createTabHolder = function (){

        // ...

    };

});

而不是这个:

(function (jQuery){
    jQuery.fn.createTabHolder = function (){

        // ...

    };

})(jQuery);
于 2013-08-07T08:59:58.710 回答
0

更换

var el = tmp.find(".tabholder");

有了这个:

var el = $(tmp.find(".tabholder"));

它现在似乎工作了,我不知道为什么,可能修复了一些奇怪的时间问题或其他东西。试错

于 2013-08-07T10:10:57.590 回答