【问题标题】:Can't get Backbone-relational to work with AMD (RequireJS)无法让骨干关系与 AMD 一起工作(RequireJS)
【发布时间】:2012-08-10 22:08:04
【问题描述】:

我在 CoffeeScript 中有以下 Backbone 路由器定义:

// appointments_router.js.coffee
define ["app", "appointment"], (App) ->
  class Snip.Routers.AppointmentsRouter extends Backbone.Router
    initialize: (options) ->
      @appointments = new Snip.Collections.AppointmentsCollection()
      @appointments.reset options.appointments

这是它所依赖的“约会”模块:

// appointment.js.coffee
define ["app", "relational"], (App) ->
  class Snip.Models.Appointment extends Backbone.RelationalModel
    paramRoot: "appointment"

    defaults:
      time_block_type_code: "APPOINTMENT"
      start_time: null
      start_time_time: null
      start_time_ymd: null
      stylist: {}
      client: {}
      notes: ''

最后,这是我的application.js.coffee

require
  paths:
    underscore: "lodash.min"
    appointment: "backbone/models/appointment"
    appointmentsRouter: "backbone/routers/appointments_router"
    relational: "backbone-relational"
  shim:
    "underscore":
      exports: "_"
    "backbone":
      deps: ["underscore"]
      exports: "Backbone"
    "relational":
      deps: ["backbone"]

requirejs ["appointmentsRouter"], (AppointmentsRouter) ->
  window.router = new Snip.Routers.AppointmentsRouter({appointments: []})
  Backbone.history.start()

当我加载页面时,我在 backbone.js 第 1019 行收到 Uncaught TypeError: undefined is not a function

如果我省略“关系”填充程序,我会在 backbone-relational.js 中得到 Uncaught TypeError: Cannot set property 'Relational' of undefined。它所说的“未定义”是Backbone。因此,如果我省略“关系”填充程序,backbone-relational.js 仍然会被加载,但它不知道 Backbone。

我该如何解决这个问题?

【问题讨论】:

    标签: javascript backbone.js coffeescript requirejs backbone-relational


    【解决方案1】:

    您可以将 shim 配置与 Require.. drop amd 一起使用,backbone 做了..

    看看 https://github.com/DarrenHurst/BroadStreet

    了解如何配置 shim。

    【讨论】:

      【解决方案2】:

      原来我需要 jQuery。

        shim:
          "underscore":
            exports: "_"
          "backbone":
            deps: ["underscore", "jquery"]
            exports: "Backbone"
          "relational":
            deps: ["backbone"]
      

      【讨论】:

      • 我讨厌用户忽略给他们答案的人,然后添加并接受他们自己的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多