Backbone.js的 - CoffeeScript的延伸 [英] Backbone.js - Coffeescript extends

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

问题描述

我在做这篇文章<一个通过链接与Backbone.js的选择href=\"http://blog.shinetech.com/2011/07/25/cascading-select-boxes-with-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的code编译为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);

,我得到了错误:

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?

推荐答案

由于您使用的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.

在换句话说,你可以告诉链轮(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天全站免登陆