簡體   English   中英

Emberjs動態段:加載路徑時出錯:TypeError {}

[英]Emberjs dynamic segment: Error while loading route: TypeError {}

我想在這里熟悉動態細分。 這是我想要實現的目標:

當我訪問'/#/ inventoryories'時,它會在'庫存'模板中列出庫存模型。 這是成功完成的。

當我點擊單個廣告資源ID時,它將訪問/#/ inventoryories / 1為1是庫存ID,它將啟動“廣告資源”模板。 這也成功完成了。

但是,當我嘗試直接從地址欄訪問/#/ inventoryories / 1時,當我按F5時,它會出現此錯誤 - Error while loading route: TypeError {}

完整的錯誤列表:

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' 

這是我的app.js

Gymi = Ember.Application.create();

// Route map
Gymi.Router.map(function() {
    this.resource('inventories', { path: '/inventories' }, function() {
        this.resource('inventory', { path: '/:inventory_id' });
    });
    this.resource('products');
});

// inventory models
Gymi.Inventory = Ember.Object.extend();
Gymi.Inventory.reopenClass({
    items: [],
    all: function() {
        this.items = [{
            id: 1,
            name: 'item1',
            cost: '20.00',
            qty: 10
        }, {
            id: 2,
            name: 'item2',
            cost: '20.00',
            qty: 10
        }, {
            id: 3,
            name: 'item3',
            cost: '20.00',
            qty: 10
        }, {
            id: 4,
            name: 'item4',
            cost: '20.00',
            qty: 10
        }];

        return this.items;
    }
})

// inventory controller
Gymi.InventoriesController = Ember.Controller.extend({
    inventories: Gymi.Inventory.all()
});

這是模板:

<script type="text/x-handlebars">
<h2>{{title}}</h2>

<ul>
    <li>{{#linkTo 'inventories'}}Inventories{{/linkTo}}</li>
</ul>

{{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="inventories">
<h2>Inventory</h2>

<table class="table">
<tbody>
{{#each inventory in inventories}}
    {{#with inventory}}
    <tr>
    <td>{{#linkTo 'inventory' inventory}}{{id}}{{/linkTo}}</td>
    <td>{{name}}</td>
    <td>{{cost}}</td>
    <td>{{qty}}</td>
    </tr>
    {{/with}}
{{/each}}
</tbody>
</table>

{{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="inventory">
<h4>Inventory</h4>

<ul>
    <li>{{id}}</li>
    <li>{{name}}</li>
    <li>{{cost}}</li>
    <li>{{qty}}</li>
</ul>
</script>

不是OP的答案,而是對於2013年9月1日之后收到錯誤的所有人,可能是由於Ember Data更新到最新的1.0版本。 所以你必須使用

this.store.find('model');

代替

App.Model.find();

在這里閱讀更多更改

這是一個無用的錯誤消息,但關鍵部分是它的結尾。

    this.init.apply(this, arguments);
} has no method 'find'

當你訪問/inventories/1路由時,ember將嘗試使用findInventoryRoutemodel鈎子中查找該id的記錄。 在這種情況下, Inventory 由於找不到該方法,因此會出現此錯誤。

添加一個返回與params.inventory_id匹配的記錄的Inventory.find方法將解決此問題。

如果您的路徑缺少model方法的參數,則會出現此錯誤。

link-to訪問/inventory/1 但不從URL打開頁面時,以下代碼有效:

App.InventoryRoute = Ember.Route.extend({
  model: function() {
    this.store.find('inventory', params.inventory_id)
  }
});

添加缺失的params修復。 此代碼可以從link-to URL直接使用:

App.InventoryRoute = Ember.Route.extend({
  model: function(params) {
    this.store.find('inventory', params.inventory_id)
  }
});

對於ember-data <0.14,這是代碼

App.InventoryRoute = Ember.Route.extend({
  model: function(params) {
    App.Inventory.find(params.inventory_id)
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM