in bootstrap.js.coffee ,there are some code:
define [], ->
App =
Models: {}
Collections: {}
Routers: {}
Helpers: {}
Views: {
Posts: {}
Common: {}
Sessions: {}
Tokens: {}
Passwords: {}
Confirmations: {}
}
i dont understand why the "define" method is in the js code?? and more , in other code ,this "define" method uses everywhere!
assets/javascripts/models/post.js.coffee
define ['backbone', 'bootstrap'], (Backbone, App) ->
class App.Models.Post extends Backbone.Model
paramRoot: 'post'
urlRoot: '/posts'
defaults:
title: null
content: null
i want to know the method --- "define" , what do the method do , why i cant find where the method details to write??
thanks !
I'm not a JavaScript person, but I believe this is part of RequireJS.
According to the docs, it appears to be defining functions with dependencies:
If the module has dependencies, the first argument should be an array of dependency names, and the second argument should be a definition function. The function will be called to define the module once all dependencies have loaded. The function should return an object that defines the module. The dependencies will be passed to the definition function as function arguments, listed in the same order as the order in the dependency array:
//my/shirt.js now has some dependencies, a cart and inventory
//module in the same directory as shirt.js
define(["./cart", "./inventory"], function(cart, inventory) {
//return an object to define the "my/shirt" module.
return {
color: "blue",
size: "large",
addToCart: function() {
inventory.decrement(this);
cart.add(this);
}
}
}
);
In this example, a my/shirt module is created. It depends on my/cart and my/inventory. On disk, the files are structured like this:
- my/cart.js
- my/inventory.js
- my/shirt.js
The function call above specifies two arguments, "cart" and "inventory". These are the modules represented by the "./cart" and "./inventory" module names.
The function is not called until the my/cart and my/inventory modules have been loaded, and the function receives the modules as the "cart" and "inventory" arguments.
Modules that define globals are explicitly discouraged, so that multiple versions of a module can exist in a page at a time (see Advanced Usage). Also, the order of the function arguments should match the order of the dependencies.
The return object from the function call defines the "my/shirt" module. By defining modules in this way, "my/shirt" does not exist as a global object.