Shim Twitter Bootstrap for RequireJS

后端 未结 4 2074
轻奢々
轻奢々 2020-12-04 10:32

The RequireJS docs say that to support older versions of IE, you need to configure enforceDefine: true.

So if you want to support Interne

4条回答
  •  暖寄归人
    2020-12-04 11:07

    I use this configuration inside my project:

    startup.js

    require.config({
        paths: {
            /* other paths are omitted */
            'bootstrap': '../libs/bootstrap'
        },
        shim: {
            'bootstrap/bootstrap-slider': { deps: ['jquery'], exports: '$.fn.slider' }, 
            'bootstrap/bootstrap-affix': { deps: ['jquery'], exports: '$.fn.affix' },
            'bootstrap/bootstrap-alert': { deps: ['jquery'], exports: '$.fn.alert' },
            'bootstrap/bootstrap-button': { deps: ['jquery'], exports: '$.fn.button' },
            'bootstrap/bootstrap-carousel': { deps: ['jquery'], exports: '$.fn.carousel' },
            'bootstrap/bootstrap-collapse': { deps: ['jquery'], exports: '$.fn.collapse' },
            'bootstrap/bootstrap-dropdown': { deps: ['jquery'], exports: '$.fn.dropdown' },
            'bootstrap/bootstrap-modal': { deps: ['jquery'], exports: '$.fn.modal' },
            'bootstrap/bootstrap-popover': { deps: ['jquery'], exports: '$.fn.popover' },
            'bootstrap/bootstrap-scrollspy': { deps: ['jquery'], exports: '$.fn.scrollspy'        },
            'bootstrap/bootstrap-tab': { deps: ['jquery'], exports: '$.fn.tab' },
            'bootstrap/bootstrap-tooltip': { deps: ['jquery'], exports: '$.fn.tooltip' },
            'bootstrap/bootstrap-transition': { deps: ['jquery'], exports: '$.support.transition' },
            'bootstrap/bootstrap-typeahead': { deps: ['jquery'], exports: '$.fn.typeahead'  },
        }
    });
    
    require(['domReady', 'app'], function(domReady, app) {
        domReady(function() {
            app.init();
        });
    });
    

    then in my code I use this:

    define(['jquery', 'underscore', 'backbone', 'text!templates/photos-list.html'], function($, _, Backbone, html) {
        var PhotosListView = Backbone.View.extend({
            viewImageFullscreen: function(e) {
                e.preventDefault();
    
                require(['bootstrap/bootstrap-modal', 'text!templates/photo-modal.html'], function(modal, htmlModal) {
                     var modalTemplate = _.template(htmlModal, options);
                     $('body').append(modalTemplate);
    
                     // setup
                     $(selector + '_modal').modal({
                         backdrop: true,
                         keyboard: true,
                         show: false
                     }).css({
                         'width': function() { return ($(document).width() * 0.55) + 'px'; },
                         'margin-left': function() { return -($(this).width() * 0.5); }
                     });
    
                     // trigger `modal` 
                     $(selector + '_modal').modal('show');
                 }); // require() call
             // ...
    

提交回复
热议问题