问题
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