你好,我正在尝试在我的ember应用程序启动时获得一些数据。
下面是我的routes/application.js文件:
export default Ember.Route.extend({
model() {
var prods = Ember.$.ajax({url: ("/products.json"), type: 'GET', dataType: 'json', success: function(e) {
var a = 1;
e.products.forEach(function(f) {
console.log(typeof(f.name));
console.log(f._id);
this.store.push({
data: [{
id: a,
type: 'product',
attributes: {
name: f.name
}
}
]
});
a = a + 1;
});
}})
}
});我基本上尝试从本地加载一些json数据,放在应用程序的公共文件夹中。在app/models/product.js中,我的模型如下所示:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string')
});最后,我的数据如下所示:
{
"products": [
{
"_id": "58ff60ffcd082f040072305a",
"slug": "apple-tree-printed-linen-butterfly-bow-tie",
"name": "Apple Tree Printed Linen Butterfly Bow Tie ",
"description": "This fun 40 Colori Apple Tree Printed Linen Butterfly Bow Tie features a design of various apple trees built from tiny polka dots. The back of this bow tie features a polka dot print in complementing colours which when the bow tie is tied will pop out from behind making for a subtle yet unique detail. The playful design, stand out natural linen texture, and colourful combinations make this bow tie a perfect accessory for any outfit!\n",
"standard_manufacturer": "58cbafc55491430300c422ff",
"details": "Size: Untied (self-tie) bow tie with an easily adjustable neck strap from 13-3/4'' to 19'' (35 to 48 cm)\nHeight: 6 cm\nMaterial: Printed 100% linen\nCare: Dry clean\nMade in Italy",诸若此类。
当到了推送商店的时候,我得到了以下错误:
application.js:32 Uncaught TypeError: Cannot read property 'store' of undefined
at application.js:32
at Array.forEach (<anonymous>)
at Object.success (application.js:29)
at fire (jquery.js:3317)
at Object.fireWith [as resolveWith] (jquery.js:3447)
at done (jquery.js:9272)
at XMLHttpRequest.<anonymous> (jquery.js:9514)请帮我解决这个问题。谢谢:)
发布于 2017-07-11 14:18:56
您可以使用箭头函数(=>) ES6语法作为此的词法作用域始终用于解决歧义。
e.products.forEach((f)=> { this.store.push()...})
https://stackoverflow.com/questions/45024315
复制相似问题