backbone.js - 使用单独文件中定义的模型加载时出现问题

发布于 2024-10-25 05:47:48 字数 900 浏览 0 评论 0原文

我正在使用 Backbone.js 运行 Sinatra。我试图分割我的模型、视图等,这样它们就不会全部集中到一个 JS 文件中。现在我有以下内容。

index.html

<html>

  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <script src="scripts/underscore-min.js"></script>
    <script src="scripts/jquery-1.5.min.js"></script>
    <script src="scripts/backbone-min.js"></script>
    <script src="scripts/models.js"></script>

  ...

models.js

Models = {

  var Event = Backbone.Model.extend({

  });


  var Events = Backbone.Collection.extend({
    url: '/events',
    model: Event
  });

};

所以 models.js 期望 Backbone.js 已被加载,它应该基于 index.html,但是,我得到了在 models.js 中引用 Backbone.Model 时出现 JavaScript 错误。

关于我在这里缺少什么有什么想法吗?

I'm running Sinatra with Backbone.js. I'm trying to split up my models, views, etc so they aren't all lumped into a single JS file. Right now I have the following.

index.html

<html>

  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <script src="scripts/underscore-min.js"></script>
    <script src="scripts/jquery-1.5.min.js"></script>
    <script src="scripts/backbone-min.js"></script>
    <script src="scripts/models.js"></script>

  ...

models.js

Models = {

  var Event = Backbone.Model.extend({

  });


  var Events = Backbone.Collection.extend({
    url: '/events',
    model: Event
  });

};

So models.js expects that Backbone.js has been loaded, which it should have been based on index.html, however, I'm getting a JavaScript error in models.js where I reference Backbone.Model.

Any ideas on what I'm missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-11-01 05:47:48

那不是有效的 JavaScript。像这样的事情更有可能起作用:

Models = {}

Models.Event = Backbone.Model.extend({

});


Models.Events = Backbone.Collection.extend({
  url: '/events',
  model: Event
});

That isn't valid javascript. Something like this is more likely to work :

Models = {}

Models.Event = Backbone.Model.extend({

});


Models.Events = Backbone.Collection.extend({
  url: '/events',
  model: Event
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文