2013-06-28 33 views
9

Tôi đang cố làm quen với phân đoạn động ở đây. Dưới đây là những gì tôi muốn đạt được:Phân đoạn động Emberjs: Lỗi khi tải tuyến đường: LoạiError {}

Khi tôi truy cập '/ #/inventory', nó sẽ liệt kê mô hình khoảng không quảng cáo trong mẫu 'khoảng không quảng cáo'. Điều này được thực hiện thành công.

Khi tôi nhấp vào id khoảng không quảng cáo riêng lẻ, nó sẽ truy cập/#/inventory/1 be 1 là id khoảng không quảng cáo và nó sẽ kích hoạt mẫu 'khoảng không quảng cáo'. Điều này cũng được thực hiện thành công.

Tuy nhiên khi tôi cố gắng để truy cập/#/hàng tồn kho/1 trực tiếp từ thanh địa chỉ, khi tôi bấm F5, nó đi ra lỗi này - Error while loading route: TypeError {}

Danh sách đầy đủ của lỗi:

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' 

đây là tôi 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() 
}); 

Dưới đây là các mẫu:

<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> 
+0

Không phải câu trả lời bạn đang tìm kiếm: Nhưng tôi nghĩ rằng nó liên quan đến các nhấp chuột định tuyến/liên kết trực tiếp khác nhau. Khi bạn nhấp vào liên kết đến '/ #/inventory/1', nó sẽ chiếm quyền điều khiển đó và chỉ tải tuyến đường. Nó hoạt động khác nhau khi bạn đi đến URL đó. Nếu bạn đang thực hiện theo cách đặt tên RESTful của URL: Bạn chỉ có thể thay đổi URL thành '/ #/inventory/1' – andy

Trả lời

2

Đó là một thông báo lỗi không hữu ích, nhưng phần chính là ở cuối của nó.

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

Khi bạn truy cập vào đường /inventories/1, ember sẽ cố gắng để tra cứu các bản ghi cho id đó, sử dụng find, trong model móc các 's InventoryRoute. Trong trường hợp này là Inventory. Vì nó không thể tìm thấy phương pháp bạn nhận được lỗi này.

Thêm phương thức Inventory.find trả về bản ghi phù hợp với params.inventory_id sẽ khắc phục sự cố này.

14

Không phải là câu trả lời cho OP nhưng cho tất cả những ai gặp lỗi sau ngày 1 tháng 9 năm 2013, có thể là do cập nhật dữ liệu Ember lên phiên bản 1.0 mới nhất. Vì vậy, bạn phải sử dụng

this.store.find('model'); 

Thay vì

App.Model.find(); 

Read more changes here.

+1

cảm ơn bạn cảm ơn bạn –

1

Lỗi này xuất hiện nếu tuyến đường của bạn thiếu thông số của phương pháp model.

Các mã sau đây hoạt động khi quý khách đến thăm /inventory/1 từ một link-tonhưng không mở trang từ URL:

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

Thêm thiếu params bản sửa lỗi. Mã này hoạt động cả hai từ một link-to và trực tiếp từ URL:

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

Đối ember dữ liệu < 0.14 đây là mã số

App.InventoryRoute = Ember.Route.extend({ 
    model: function(params) { 
    App.Inventory.find(params.inventory_id) 
    } 
}); 
Các vấn đề liên quan