Backbone.js - Coffeescript 扩展 [英] Backbone.js - Coffeescript extends

查看:16
本文介绍了Backbone.js - Coffeescript 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过这篇文章使用backbone.js进行链接选择http://blog.shinetech.com/2011/07/25/cascading-select-boxes-with-backbone-js/,但在扩展类时出错.

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.

所以,我有 LocationsView 类:

So, i have LocationsView class:

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

CountriesView 类:

CountriesView class:

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

CitiesView 类:

CitiesView class:

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

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

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);

我得到了错误:

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

那么,问题出在哪里以及如何解决?

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

推荐答案

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

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.

当文件在同一文件夹中时,3.1 中的资产管道将按字母顺序显示您的 js 文件.

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

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

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.

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

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

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

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

##= require ./locations_view

祝你好运

这篇关于Backbone.js - Coffeescript 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆