Backbone.js - Coffeescript 扩展

发布于 2024-12-13 06:18:59 字数 1635 浏览 0 评论 0原文

我通过这篇文章 http: //blog.shinetech.com/2011/07/25/cascading-select-boxes-with-backbone-js/,但在扩展类时出现错误。

所以,我有 LocationsView 类:

class Blog.Views.LocationsView extends Backbone.View
  events:
    "change": "changeSelected"

CountrysView 类:

class Blog.Views.CountriesView extends Blog.Views.LocationsView
  setSelectedId: (countryId) ->

CitiesView 类:

class Blog.Views.CitiesView extends Blog.Views.LocationsView
  setSelectedId: (cityId) ->

但是当 CoffeeScript 代码编译为 javascript 时,我的双扩展类看起来像:

(function() {
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
cities_view.js:5 Uncaught TypeError: Cannot read property 'prototype' of undefined
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  Blog.Views.CitiesView = (function() {
    __extends(CitiesView, Blog.Views.LocationsView);
    function CitiesView() {
      CitiesView.__super__.constructor.apply(this, arguments);
    }
    CitiesView.prototype.setSelectedId = function(cityId) {};
    return CitiesView;
  })();
}).call(this);

我得到了错误:

Uncaught TypeError: Cannot read property 'prototype' of undefined    cities_view.js:5

那么,问题出在哪里以及如何修复它?

I'm making chaining selects with backbone.js by this article http://blog.shinetech.com/2011/07/25/cascading-select-boxes-with-backbone-js/, but got errors, when extending classes.

So, i have LocationsView class:

class Blog.Views.LocationsView extends Backbone.View
  events:
    "change": "changeSelected"

CountriesView class:

class Blog.Views.CountriesView extends Blog.Views.LocationsView
  setSelectedId: (countryId) ->

CitiesView class:

class Blog.Views.CitiesView extends Blog.Views.LocationsView
  setSelectedId: (cityId) ->

But when coffeescript code compiled to javascript my double extended classes looks like:

(function() {
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
cities_view.js:5 Uncaught TypeError: Cannot read property 'prototype' of undefined
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  Blog.Views.CitiesView = (function() {
    __extends(CitiesView, Blog.Views.LocationsView);
    function CitiesView() {
      CitiesView.__super__.constructor.apply(this, arguments);
    }
    CitiesView.prototype.setSelectedId = function(cityId) {};
    return CitiesView;
  })();
}).call(this);

And i got error:

Uncaught TypeError: Cannot read property 'prototype' of undefined    cities_view.js:5

So, where the problem is and how to fix it?

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

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

发布评论

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

评论(2

窗影残 2024-12-20 06:18:59

既然您使用的是 ROR,那么您在资产管道中使用 3.1 是否正确?如果您没有使用 3.1,那么此信息可能仍然有用,具体取决于您的操作方式。

当文件位于同一文件夹中时,3.1 中的资源管道将按字母顺序排列您的 js 文件。

因此,city_view.js 将在locations_view.js 之前执行。然后,当 CitiesView 尝试定义自身时,LocationsView 还不存在。 (但这让我有点困惑,因为您不应该使用 .coffee 文件而不是 .js 文件吗?)

您将不得不破坏资产管道中文件的顺序(可通过注释控制)以获得正确的结果文件执行...或更改名称。

换句话说,您可以告诉 Sprockets(RoR 中管理资产管道的东西)首先需要另一个文件。

cities_view.coffee 文件的顶部,您可以添加以下行:

##= require ./locations_view

祝你好运

Since you are using ROR, is it correct to say that you are using 3.1 with the asset pipeline? If you are not using 3.1, then this info might still be useful, depending on how you are doing things.

The asset pipeline in 3.1 will bring your js files in alphabetical order when the files are within the same folder.

Because of that, cities_view.js will be executed before locations_view.js. Then, when CitiesView tries to define itself, LocationsView doesn't exist yet. (But this confuses me a bit because shouldn't you be using .coffee files instead of .js files?)

You will have to muck with the order of the files in the asset pipeline (controllable via comments) in order to get the correct file executed... or change the names.

In other words, you can tell Sprockets (the thing in RoR which manages your asset pipeline) to require the other file first.

At the top of your cities_view.coffee file, you can add the following line:

##= require ./locations_view

Good luck

樱花落人离去 2024-12-20 06:18:59

正如 @brian Genisio 所说,问题在于 ROR 资产管道中文件加载的字母顺序。

我发现将所有继承自其他模型的模型放在子目录中很有用。这样,ROR 首先加载父目录中的所有文件,然后再加载子目录中的文件。对于读者来说,它也显得更合乎逻辑。

例如,同一目录中的 vehicle.jscar.js (其中 car 扩展了vehicle)将不起作用,因为 car.js 已加载并在 vehicle.js 之前运行,并且无法继承它。

car.js 放入子目录(例如 vehicle_models/car.js)即可。

As @brian Genisio says, Its the alphabetical ordering of file loading in ROR's asset pipeline that's the issue.

I've found it useful to put all models that inherit from others in a sub-directory. This way, ROR loads all files in the parent directory first, before loading files in the sub-directory. It also appears more logical to the reader.

e.g. vehicle.js and car.js (where car extends vehicle) in the same directory wouldn't work, as car.js is loaded and run before vehicle.js, and isn't able to inherit from it.

Putting car.js in a sub directory (e.g. vehicle_models/car.js) would then work.

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