首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember.js: forEach中的this.store.push

Ember.js: forEach中的this.store.push
EN

Stack Overflow用户
提问于 2017-07-11 10:20:05
回答 1查看 152关注 0票数 0

你好,我正在尝试在我的ember应用程序启动时获得一些数据。

下面是我的routes/application.js文件:

代码语言:javascript
复制
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中,我的模型如下所示:

代码语言:javascript
复制
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string')
});

最后,我的数据如下所示:

代码语言:javascript
复制
{
  "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",

诸若此类。

当到了推送商店的时候,我得到了以下错误:

代码语言:javascript
复制
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)

请帮我解决这个问题。谢谢:)

EN

回答 1

Stack Overflow用户

发布于 2017-07-11 14:18:56

您可以使用箭头函数(=>) ES6语法作为的词法作用域始终用于解决歧义。

e.products.forEach((f)=> { this.store.push()...})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45024315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档