javascript – Emberjs动态段:加载路由时出错:TypeError {}

前端之家收集整理的这篇文章主要介绍了javascript – Emberjs动态段:加载路由时出错:TypeError {}前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在这里熟悉动态片段.这是我想要实现的:

当我访问“/#/库存”时,它将在“库存”模板中列出库存模型.这是成功的.

当我点击个别库存ID时,它将访问/#/ inventory / 1为1是库存ID,它将启动“库存”模板.这也是成功的.

但是,当我尝试从地址栏直接访问/#/ inventory / 1时,当我按F5时,出现这个错误 – 加载路由时出错: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,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',{
            id: 3,name: 'item3',{
            id: 4,name: 'item4',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>

解决方法

这是一个无益的错误消息,但关键部分是结束.
this.init.apply(this,arguments);
} has no method 'find'

当您访问/ inventory / 1路由时,ember会尝试在InventoryRoute的模型钩子中使用find查找该id的记录.在这种情况下在库存.由于找不到该方法,您会收到此错误.

添加一个Inventory.find方法返回与params.inventory_id匹配的记录将修复此问题.

原文链接:https://www.f2er.com/js/152253.html

猜你在找的JavaScript相关文章