2

我有一个包含服务器表的 ember-rails 应用程序,以及到每个单独服务器的链接。

这是主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}}

正如预期的那样,它创建了适当的表。当悬停在服务器名称上时,我将在浏览器的左下角收到 url: #/servers/1,#/servers/2

但是,当我单击指向单个服务器的链接时,不会呈现服务器模板。这是我的server.handlebars文件:

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

以下是我server.js.coffee位于 javascripts/models 文件夹中的文件:

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

我的serversRoute.js.coffee文件:

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

我的serverRoutes.js.coffee文件:

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

最后是router.js.coffee文件

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

请注意,Chrome 中的 ember 工具箱没有显示任何路由,并且只有在尝试导航到单个服务器时才会在控制台中引发错误。下面是错误。

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' 
4

1 回答 1

3

短的

这是将 coffe 与 ember 一起使用时的常见错误,可能您使用SomeEmberObject.extend的是SomeEmberObject.extend().

长的

当您Foo = SomeEmberObject.extend不使用 args 时,coffee 会生成以下代码:

var Foo;

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

因为Foo只是扩展的引用,所以当Foo.create()被调用时,这是一样的SomeEmberObject.extend.create(),会抛出你当前的错误。

观察:即使您没有在代码中直接调用 create,也使用您声明的类,并在执行依赖注入时调用 create。

使用 args,可以正确调用 extend 函数:

Foo = SomeEmberObject.extend
  message: 'hello'

生成:

var Foo;

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

因此,您需要在代码中替换声明Foo = SomeEmberObject.extendFoo = SomeEmberObject.extend().

例如:

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

// correct
Warthog.ServerController = Ember.ObjectController.extend()
于 2013-11-23T19:40:19.827 回答