Backbone-localstorage 不适用于 require.js

发布于 2025-01-07 17:16:12 字数 660 浏览 1 评论 0原文

我正在尝试将本地存储添加到backbone.js 中的集合中,但由于某种原因,require.js 不会加载它。

以下是 requirejs 加载的 main.js 文件中的内容:

require.config({
  paths: {
    'jquery': 'libs/jquery/jquery-1.7.1.min',
    'underscore': 'libs/underscore/underscore-min',
    'backbone': 'libs/backbone/backbone-min',
    'backbone-localstorage': 'libs/backbone-localstorage/backbone-localstorage-min',
    'text': 'libs/require/text'
  }
});

您可以在 查看完整源代码https://github.com/tomulated/Vendotron。我可以看出它没有加载,因为当我将 localstorage 片段放入我的集合中时,它在 chrome 控制台中出错,提示 Store 未定义。

知道我做错了什么吗?

I'm trying to add localstorage to my collections in backbone.js, but for some reason, require.js wont load it.

Here's what is in the main.js file that requirejs loads:

require.config({
  paths: {
    'jquery': 'libs/jquery/jquery-1.7.1.min',
    'underscore': 'libs/underscore/underscore-min',
    'backbone': 'libs/backbone/backbone-min',
    'backbone-localstorage': 'libs/backbone-localstorage/backbone-localstorage-min',
    'text': 'libs/require/text'
  }
});

You can see the full source at https://github.com/tominated/Vendotron. I can tell it's not loading because when I put the localstorage snippet into my collection, it errors out in chrome's console saying that Store isn't defined.

Any idea what I'm doing wrong?

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

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

发布评论

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

评论(3

べ映画 2025-01-14 17:16:12

正如保罗所说,您不需要任何地方的本地存储模块。 Require.js 2.0 有一个针对 JavaScript 代码的特定机制,本质上是其他代码的插件 - shim 选项。包括 localStorage 看起来像这样:

require.config({
  baseUrl: "/js/",
  paths: {
    jquery: 'lib/jquery-1.8.0',
    underscore: 'lib/underscore-1.3.3',
    backbone: 'lib/backbone-0.9.2',
    'backbone.localStorage': 'lib/backbone.localStorage'
  },
  shim: {
    underscore: {
      exports: "_"
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    'backbone.localStorage': {
      deps: ['backbone'],
      exports: 'Backbone'
    }
  }
});

此示例是从文章“

As Paul said, you are not requiring the localstorage module anywhere. Require.js 2.0 has a specific mechanism for JavaScript code that is essentially a plugin for other code - the shim option. Including localStorage would look like this:

require.config({
  baseUrl: "/js/",
  paths: {
    jquery: 'lib/jquery-1.8.0',
    underscore: 'lib/underscore-1.3.3',
    backbone: 'lib/backbone-0.9.2',
    'backbone.localStorage': 'lib/backbone.localStorage'
  },
  shim: {
    underscore: {
      exports: "_"
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    'backbone.localStorage': {
      deps: ['backbone'],
      exports: 'Backbone'
    }
  }
});

This example was copied from the article "Build Backbone Apps Using RequireJS" which also explains how to structure your code and how to compile the code into one file when deploying the application.

甜扑 2025-01-14 17:16:12

有几个问题。

首先,您正在设置主干本地存储的路径,但您永远不会在任何地方需要它,因此它永远不会被实际加载。设置该路径基本上是定义它的快捷方式,而不是加载它。

第二个问题是,就像主干本身一样,大多数主干插件都不是 AMD 模块。他们希望首先加载 Backbone,以便可以向其中添加扩展。

看起来您正在使用 Backbone 的 AMD 分支,但不是骨干本地存储。您可以尝试找到一个现有的,或者制作您自己的 类似的对此

或者,或者您可以尝试按原样加载backbone-localstorage(添加到定义调用的依赖项列表中),但您需要使用!order插件来确保首先加载backbone。

There are a couple problems.

First, you are setting the path to backbone-localstorage, but you are never requiring it anywhere, so it is never actually loaded. Setting that path is basically defining a shortcut to it, not loading it.

The second problem is that, like backbone itself, most backbone plugins are not AMD modules. They want to have Backbone loaded first, so they can add their extensions to it.

It looks like you are using an AMD fork of Backbone, but not backbone-localstorage. You could try to find an existing one, or make your own similar to this.

Either that, or you can try to load backbone-localstorage as-is (adding to the dependencies list of your define call), but you would need to use the !order plugin to make sure backbone is loaded first.

墨洒年华 2025-01-14 17:16:12

在查看源代码内部时,需要下划线和主干的地方,需要配置中的路径定义应与本地存储中所需的路径一致,即区分大小写

On looking inside the source code, where underscore and backbone are required, your path definition in require config should tally with the required path in local storage I.e case sensitive

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文