javascript - 一些代码让我在这个 gem 中感到困惑 ----backbone-bootstrap-rails

标签 javascript ruby-on-rails rubygems twitter-bootstrap-rails

在bootstrap.js.coffee中,有一些代码:

define [], ->
  App =
    Models: {}
    Collections: {}
    Routers: {}
    Helpers: {}
    Views: {
      Posts: {}
      Common: {}
      Sessions: {}
      Tokens: {}
      Passwords: {}
      Confirmations: {}
    }

我不明白为什么“define”方法出现在js代码中? 而且,在其他代码中,这个“define”方法到处都在使用!

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

我想知道这个方法---“定义”,这个方法是做什么的,为什么我找不到在哪里写方法细节?

谢谢!

最佳答案

我不是 JavaScript 爱好者,但我相信这是 RequireJS 的一部分。

根据the docs ,它似乎正在定义具有依赖关系的函数:

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.

关于javascript - 一些代码让我在这个 gem 中感到困惑 ----backbone-bootstrap-rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250854/

相关文章:

javascript - 部署 Next.js

c# - 如何从图像尺寸计算纵横比

ruby-on-rails - 在 rspec 中找不到 Font-awesome-rails 帮助程序

ruby-on-rails - ActionMailer 将局部变量传递给 erb 模板

ruby-on-rails - 当我尝试更新 Gem 时遇到问题,服务器没有返回有效文件

javascript - Grails用 Angular 上传文件

javascript - 在 paperJS 中将自定义对象添加到 Group 中

ruby-on-rails - 使用 textmate 重构 Rails 应用程序时如何查找引用?

ruby - 我怎样才能完全重置我的 Ruby 安装?

ruby-on-rails - 在environment.rb中的config.gem调用中 `lib`是什么意思