0

在构建在 Rails 后端的 ember 应用程序中,我总是遇到这个问题。我有一个groups.hbs 模板,其中列出了一堆组,当您单击一个组时,它会在组模板旁边加载group.hbs 模板并将url 更改为/groups/:group_id。

但是,当我单击后退和前进按钮,或尝试手动加载具有特定 :group_id 的 url 时,组模板无法呈现并且控制台会抛出一个巨大的

Uncaught TypeError: Object function () {
  ...

错误。

group.js.coffee

App.Group = Ember.Object.extend()

App.Group.reopenClass
  all: ->
    App.ajax(
      url: App.apiUrl('/groups')
    ).then (data) ->
      console.log data
      groups = []
      for group in data.response
        groups.addObject(App.Group.create(group))
      console.log(groups)
      groups

router.js.coffee

Mdm.Router.map ->
  @resource 'groups', ->
    @resource 'group', {path: "/:group_id"}

Mdm.Router.reopen
  location: 'history'

在构建独立的 ember 应用程序时,我从未遇到过这个问题。知道什么会导致这种情况吗?

编辑

我应该补充一点,我正在通过 XHR 请求从 api 中提取数据。

编辑 2

我只是显式地创建了 GroupRoute 并让它加载所有组,此代码与 GroupsRoute 相同。模板仍未呈现,但我不再收到该错误。

组路由

App.GroupRoute = Ember.Route.extend(model: ->
  App.Group.all()
)

和 GroupsRoute:

App.GroupsRoute = Ember.Route.extend(model: ->
  App.Group.all()
)

编辑 3

如果它对任何人有帮助,这就是整个错误。

Uncaught TypeError: Object function () {
    if (!wasApplied) {
      Class.proto(); // prepare prototype...
    }
    o_defineProperty(this, GUID_KEY, undefinedDescriptor);
    o_defineProperty(this, '_super', undefinedDescriptor);
    var m = meta(this);
    m.proto = this;
    if (initMixins) {
  // capture locally so we can clear the closed over variable
  var mixins = initMixins;
  initMixins = null;
  this.reopen.apply(this, mixins);
}
if (initProperties) {
  // capture locally so we can clear the closed over variable
  var props = initProperties;
  initProperties = null;

  var concatenatedProperties = this.concatenatedProperties;

  for (var i = 0, l = props.length; i < l; i++) {
    var properties = props[i];

    Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));

    for (var keyName in properties) {
      if (!properties.hasOwnProperty(keyName)) { continue; }

      var value = properties[keyName],
          IS_BINDING = Ember.IS_BINDING;

      if (IS_BINDING.test(keyName)) {
        var bindings = m.bindings;
        if (!bindings) {
          bindings = m.bindings = {};
        } else if (!m.hasOwnProperty('bindings')) {
          bindings = m.bindings = o_create(m.bindings);
        }
        bindings[keyName] = value;
      }

      var desc = m.descs[keyName];

      Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
      Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));

      if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
        var baseValue = this[keyName];

        if (baseValue) {
          if ('function' === typeof baseValue.concat) {
            value = baseValue.concat(value);
          } else {
            value = Ember.makeArray(baseValue).concat(value);
          }
        } else {
          value = Ember.makeArray(value);
        }
      }

      if (desc) {
        desc.set(this, keyName, value);
      } else {
        if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
          this.setUnknownProperty(keyName, value);
        } else if (MANDATORY_SETTER) {
          Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
        } else {
          this[keyName] = value;
        }
      }
    }
  }
}
finishPartial(this, m);
delete m.proto;
finishChains(this);
this.init.apply(this, arguments);
  } has no method 'find'

编辑

所以我想我找到了问题所在,当您单击后退按钮或输入手动 url 时,它没有找到基于 id 的对象。所以我在 Group 模型中添加了一个 find() 方法。不是这样的:

Mdm.Group = Ember.Object.extend()

Mdm.Group.reopenClass
  all: ->
    Mdm.ajax(
      url: Mdm.apiUrl('/groups')
    ).then (data) ->
      console.log data
      groups = []
      for group in data.response
        groups.addObject(Mdm.Group.create(group))
      console.log(groups)
      groups

  find: (group_id) ->
    Mdm.ajax(
      url: Mdm.apiUrl("/groups/#{group_id}")
    ).then (data) ->
      renderTemplate: (data)

我的 GroupRoute 看起来像这样:

Mdm.GroupRoute = Ember.Route.extend
  model: (params) ->
    console.log 'oh hai'
    Mdm.Group.find(params.group_id)

现在在控制台中,当我单击后退按钮时,它正在加载数据,但它没有将组模板与 group_id 关联。告诉 ember 执行此操作的最佳实践方法是什么?

4

1 回答 1

0

I'm not a rails developer but try doing something like this for the simple model/route setup you show above

App.Group = Ember.Object.extend().reopenClass
  groups: []
  find: ->
    $.ajax
      url: "/api/groups/"
      type: "GET"
      cache: false
      dataType: "json"
      beforeSend: =>
        @groups.clear()
      success: (results) =>
        [@groups.addObject(App.Group.create(result)) for result in results]
      error: =>
        alert "error: failed to load the available groups"
    @groups

App.Router.map ->
  @resource "groups", path: "/", ->
    @route "group", path: "/:group_id"
于 2013-06-19T11:37:16.270 回答