Can't get Backbone-relational to work with AMD (RequireJS)

我怕爱的太早我们不能终老 提交于 2019-12-12 04:23:36

问题


I have the following Backbone router definition in CoffeeScript:

// 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

Here is the "appointment" module on which it depends:

// 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: ''

And finally, here is my 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()

When I load the page, I get Uncaught TypeError: undefined is not a function on backbone.js, line 1019.

If I omit the "relational" shim, I instead get Uncaught TypeError: Cannot set property 'Relational' of undefined in backbone-relational.js. The "undefined" it's talking about is Backbone. So if I omit the "relational" shim, backbone-relational.js still gets loaded, but it doesn't know about Backbone.

How do I fix this?


回答1:


You can use a shim configuration with Require.. drop amd, backbone did..

take a look at https://github.com/DarrenHurst/BroadStreet

for how to configure shim.




回答2:


Turns out I need to require jQuery.

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


来源:https://stackoverflow.com/questions/11955309/cant-get-backbone-relational-to-work-with-amd-requirejs

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