简体   繁体   English

嵌套路由错误Ember.JS和Rails

[英]Nested Routing Error Ember.JS and Rails

I have an ember-rails app that contains a table of servers, and a link to each individual server. 我有一个ember-rails应用程序,其中包含服务器表以及指向每个服务器的链接。

Here is the main servers.handlebars file: 这是主要的servers.handlebars文件:

<table id="box-table-a">
    <thead> 
        <th>Name</th> 
        <th>Operating System</th> 
        <th>Build Stage</th> 
    </thead> 
    <tbody> 
        {{#each server in controller}} 
        <tr> 
            <td>
                {{#link-to 'server' server}}
                    {{server.name}}
                {{/link-to}}
            </td> 
            <td>{{server.operating_system}}</td> 
            <td>{{server.build_stage}}</td> 
        </tr> 
        {{/each}}
    </tbody>
</table>

{{outlet}}

Which creates the appropriate table, as expected. 它将创建适当的表,如预期的那样。 And when hovering over a servers name I will receive the url in in the bottom left corner of the browser: #/servers/1 , #/servers/2 , etc 当将鼠标悬停在服务器名称上时,我将在浏览器的左下角收到网址: #/servers/1#/servers/2 ,等等

However, when I click the link to an individual server, the server template is not rendered. 但是,当我单击指向单个服务器的链接时,不会呈现服务器模板。 Here is my server.handlebars file: 这是我的server.handlebars文件:

<div>
    <h2> {{name}} </h2>
    <h2> {{operating_system}} </h2>
    <h2> {{build_stage}} </h2>
</div>

Below is my server.js.coffee file located in the javascripts/models folder: 以下是我的server.js.coffee文件,位于javascripts / models文件夹中:

Warthog.Server = DS.Model.extend
  name: DS.attr('string')
  operating_system: DS.attr('string')
  build_stage: DS.attr('string')

My serversRoute.js.coffee file: 我的serversRoute.js.coffee文件:

Warthog.ServersRoute = Ember.Route.extend
  model: -> @store.find('server')

My serverRoutes.js.coffee file: 我的serverRoutes.js.coffee文件:

Warthog.ServerRoute = Ember.Route.extend
    model: (params) ->
        @store.find('server', params.server_id)

Last is the router.js.coffee file 最后是router.js.coffee文件

Warthog.Router.map ->
  @resource "servers", ->
    @resource "server", path: "/:server_id", ->
      @route "edit"
    @route "create"

Please note that the ember toolbox within Chrome is not showing any routes and that an error is only thrown in the console when attempting to navigate to an individual server. 请注意,Chrome中的ember工具箱未显示任何路线,并且仅在尝试导航到单个服务器时在控制台中引发错误。 Below is the error. 下面是错误。

event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery.js?body=1:5387
Ember Debugger Active VM3178:391
DEPRECATION: Action handlers contained in an `events` object are deprecated in favor of putting them in an `actions` object (error on <Ember.Route:ember430>)
        at Object.triggerEvent (http://testdomain.warthog.dev/assets/ember.js?body=1:30519:13)
        at trigger (http://testdomain.warthog.dev/assets/ember.js?body=1:29641:16)
        at handlerEnteredOrUpdated (http://testdomain.warthog.dev/assets/ember.js?body=1:29537:11)
        at http://testdomain.warthog.dev/assets/ember.js?body=1:29512:9
        at eachHandler (http://testdomain.warthog.dev/assets/ember.js?body=1:29559:9)
        at setupContexts (http://testdomain.warthog.dev/assets/ember.js?body=1:29511:7)
        at finalizeTransition (http://testdomain.warthog.dev/assets/ember.js?body=1:29835:7)
        at transitionSuccess (http://testdomain.warthog.dev/assets/ember.js?body=1:29732:13)
        at invokeCallback (http://testdomain.warthog.dev/assets/ember.js?body=1:8055:19) ember.js?body=1:394
Error while loading route: TypeError {} ember.js?body=1:394
Uncaught TypeError: Object function () {
    var Class = makeCtor(), proto;
    Class.ClassMixin = Mixin.create(this.ClassMixin);
    Class.PrototypeMixin = Mixin.create(this.PrototypeMixin);

    Class.ClassMixin.ownerConstructor = Class;
    Class.PrototypeMixin.ownerConstructor = Class;

    reopen.apply(Class.PrototypeMixin, arguments);

    Class.superclass = this;
    Class.__super__  = this.prototype;

    proto = Class.prototype = o_create(this.prototype);
    proto.constructor = Class;
    generateGuid(proto, 'ember');
    meta(proto).proto = proto; // this will disable observers on prototype

    Class.ClassMixin.apply(Class);
    return Class;
  } has no method 'create' 

The short

This is a commom error when using coffe with ember, probally you are using SomeEmberObject.extend instead of SomeEmberObject.extend() . 当使用余烬咖啡时,这是一个常见错误,可能是您使用SomeEmberObject.extend而不是SomeEmberObject.extend()

The long

When you use Foo = SomeEmberObject.extend without args, coffee generate the following code: 当您使用不带参数的Foo = SomeEmberObject.extend ,coffee会生成以下代码:

var Foo;

// Foo is a reference to extend, this is not what we want 
Foo = SomeEmberObject.extend;

Because Foo is just a refence to extend, when Foo.create() is called, this is the same like SomeEmberObject.extend.create() and will throw your current error. 因为Foo只是扩展的参考,所以在Foo.create()时,它与SomeEmberObject.extend.create()相同,并且会抛出当前错误。

Obervation: even you not calling create directlly in your code, ember use your declared classes, and call create when performing the dependency injection. 观察:即使您没有在代码中直接调用create,也没有使用您声明的类,并且在执行依赖注入时调用create。

With args, the extend function is called correctlly: 使用args时,extendly函数会正确调用:

Foo = SomeEmberObject.extend
  message: 'hello'

Generates: 产生:

var Foo;

// ok
Foo = SomeEmberObject.extend({
  message: 'hello'
});

So you need to replace in your code, classes declared like Foo = SomeEmberObject.extend to Foo = SomeEmberObject.extend() . 因此,您需要在代码中替换声明为Foo = SomeEmberObject.extend类,将其替换为Foo = SomeEmberObject.extend()

For example: 例如:

// wrong
Warthog.ServerController = Ember.ObjectController.extend

// correct
Warthog.ServerController = Ember.ObjectController.extend()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM