require.js alternative way to require?

吃可爱长大的小学妹 提交于 2019-12-12 16:09:21

问题


New to require.js. I have this:

define([
    'jquery',
    'underscore',
    'backbone',
    'views/sidebar',
    'views/dashboard',
    'views/users',
    'views/venues',
    'views/payments'
], function($, _, Backbone, SidebarView, DashboardView, UsersView, VenuesView, PaymentsView) {

    var Router = Backbone.Router.extend({
        routes: {
            '/dashboard': 'showDashboard',
            '/users': 'showUsers',
            '/venues': 'showVenues',
            '/payments': 'showPayments'
        }
    });

    var initialize = function() {
        var router = new Router();
        Backbone.history.start();
    }

    return {
        initialize: initialize
    };
});

I am wondering if there's an alternative way to write this so I don't have this long line:

function($, _, Backbone, SidebarView, DashboardView, UsersView, VenuesView, PaymentsView) 

回答1:


You can use AMD sugar:

define(function(require) {

  var $ = require('jquery')
    , _ = require('underscore')
    , Backbone = require('backbone')

    // etc...

I personally prefer this because it’s closer to CommonJS syntax, but there are some browser limitations (it requires Function.prototype.toString) so make sure you pack it using r.js before production.

More examples in the docs: http://requirejs.org/docs/whyamd.html#sugar




回答2:


Or you can try more generic and natural solution (not only for JS) http://sourceforge.net/projects/minimerge

It supports full dependencies stack, multiple source trees and is purely java based (and you can use it for content filtering too).

Particulary you end up with:

//:include com/package/Class.js

//any code.




回答3:


minimerge is a plain java tool, few KB small and super fast. I use it for my html5 web projects. It also does CSS with dependencies and any text based files ;-)



来源:https://stackoverflow.com/questions/20284265/require-js-alternative-way-to-require

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!