如何将 Backbone 模型与 Node.js 和 AMD 结合使用(浏览器上的 require.js)

发布于 2024-12-15 21:56:53 字数 2008 浏览 1 评论 0原文

我目前有一个使用 require.js 的工作 AMD 主干模型,如下所示:

// models/game.js
define(['backbone'],
function(Backbone) {

  var Game = Backbone.Model.extend({
    urlRoot  : '/games/'
  , defaults : {
      name : null
    }
  });

  return Game;
});

AMD/Backbone 组织来了来自本教程jrburke 的 Backbone 拉取请求

我也想在 Node.js 中使用 Backbone 模型,因为过去不使用 AMD 时共享 Backbone 模型和集合效果很好,而且显然我是一个受虐狂。

所以我尝试了以下操作(受到 Backbone mod 的启发):

// models/game.js
(function(root, factory) {
  if (typeof exports !== 'undefined') {
    factory(root, exports, require('backbone'));
  }
  else if (typeof define === 'function' && define.amd) {
    define(['backbone'], function(Backbone, exports) {
      factory(root, exports, Backbone);
    });
  }
}(this, function(root, Game, Backbone) {

  Game = Backbone.Model.extend({
    urlRoot  : '/games/'
  , defaults : {
      name : null
    }
  });

  return Game;
}));

但是当我将它包含在浏览器中时, Game 现在是未定义的:

// collections/games.js
define(['backbone', 'models/game'],
function(Backbone, Game) {

  var Games = Backbone.Collection.extend({
    model: Game

  , initialize: function() {
      console.log(Game) 
      // Game is undefined
      var game = new Game({ name: 'game1' });
    }
  });

  return Games;
});

当我查看 CommonJS笔记,恐怕我还不清楚。 如何在浏览器中使用相同的 Backbone 模型文件作为 AMD 文件和 Node.js 模块?

还有额外的好处:有没有比 ~10 更简洁的方法每个文件顶部的行? 理想情况下没有 定义垫片

I currently have a working AMD Backbone Model using require.js like so:

// models/game.js
define(['backbone'],
function(Backbone) {

  var Game = Backbone.Model.extend({
    urlRoot  : '/games/'
  , defaults : {
      name : null
    }
  });

  return Game;
});

The AMD/Backbone organization comes from this tutorial and jrburke's Pull Request for Backbone.

I'd like to use the Backbone Model in Node.js too, because sharing Backbone models and collections has worked well in the past when not using AMD and, well, apparently I'm a masochist.

So I tried the following (inspired by the Backbone mod):

// models/game.js
(function(root, factory) {
  if (typeof exports !== 'undefined') {
    factory(root, exports, require('backbone'));
  }
  else if (typeof define === 'function' && define.amd) {
    define(['backbone'], function(Backbone, exports) {
      factory(root, exports, Backbone);
    });
  }
}(this, function(root, Game, Backbone) {

  Game = Backbone.Model.extend({
    urlRoot  : '/games/'
  , defaults : {
      name : null
    }
  });

  return Game;
}));

But Game is now undefined when I include it in the browser:

// collections/games.js
define(['backbone', 'models/game'],
function(Backbone, Game) {

  var Games = Backbone.Collection.extend({
    model: Game

  , initialize: function() {
      console.log(Game) 
      // Game is undefined
      var game = new Game({ name: 'game1' });
    }
  });

  return Games;
});

While I looked at the CommonJS notes, I'm afraid I'm still unclear. How do I use the same Backbone Model file as an AMD file in the browser and as a Node.js module?

And for bonus: Is there a cleaner way than the ~10 lines at the top of each file? Ideally without the define shim.

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

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

发布评论

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

评论(1

深白境迁sunset 2024-12-22 21:56:53

您是否也尝试过在节点上以 AMD 方式执行此操作?

http://requirejs.org/docs/node.html - 如果您愿意,可能是最好的解决方案在客户端和后端具有相同的 AMD 模块。

Have you tried doing it AMD way on node as well?

http://requirejs.org/docs/node.html - might be best solution if you want to have the same AMD modules on both client and backend side.

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