使用forEach()的Ember访问模型数组

时间:2016-10-12 16:08:09

标签: javascript arrays ember.js ember-data

我在将0.X格式的ember-data升级到1.0.0-beta12时遇到问题。我的ember.js版本是1.7.1。在我的控制器中,我有一个方法contentChanged():循环我的模型:

this.get('model').forEach(function(story) {
    mutableComments.pushObject(story);
});

由于升级了ember-data,this.get('model');的结构发生了变化,因此forEach()找不到我的数组。

更新前: Before

更新后:

After

内容最初是array[20],但现在它是一个具有内容属性array[20]的类。我需要遍历这些项目,但当前的代码不起作用。

我试过了。 this.get('model').toArray()但是这返回了一个空数组。如何访问内容数组?

编辑:要添加更多上下文,应用程序使用此代码迭代故事对象并将它们放在currentStories数组中。我们使用包含feedSettings的Ember.Mixin执行此操作,并使用我们要创建Feed的Mixin。

    App.FeedRouteMixin = Ember.Mixin.create({
        /* When using the FeedRouteMixin, create a feedSettings object in the
         * route with any of the following properties. If a property is missing,
         * it will take the default value expressed below.

        feedSettings: {
            mountPoint: [],
            canCompose: true,
            firstLoad: true,
            ableToLoadMore: true,
            hasNewStories: false,
            complexFeed: true,
            feedType: "course"      // "course", "home", "profile" "announcement"
            canAnnounce: false,
            isAnnouncement: false,
            content: [],
            into: "user"
        },*/
        /**
         * Renders the feed using the feedSettings
         */
        renderFeed: function() {
            var fs = this.get("feedSettings");
            var feedController = this.controllerFor('feed');
            feedController.set('mountPoint', fs.mountPoint);
            feedController.set('canCompose', (typeof fs.canCompose != "undefined") ? fs.canCompose : true);
            feedController.set('firstLoad', (typeof fs.firstLoad != "undefined") ? fs.firstLoad : true);
            feedController.set('ableToLoadMore', (typeof fs.ableToLoadMore != "undefined") ? fs.ableToLoadMore : true);
            feedController.set('hasNewStories', (typeof fs.hasNewStories != "undefined") ? fs.hasNewStories : false);
            feedController.set('complexFeed', (typeof fs.complexFeed != "undefined") ? fs.complexFeed : true);
            feedController.set('feedType', (typeof fs.feedType != "undefined") ? fs.feedType : "course");
            feedController.set('is_announcement', (typeof fs.isAnnouncement != "undefined") ? fs.isAnnouncement : false);
            feedController.set('content', fs.content);

            this.render("feed", {
                outlet: "feed",
                into: fs.into,
                controller: feedController
            });
        },
        renderTemplate: function(controller, model){
            this.render();
            this.renderFeed();
        }
    });

这是我们使用mixin的地方:

App.HomeRoute = Ember.Route.extend(App.FeedRouteMixin, App.FlagMixin, {
    feedSettings: {
        into: "home",
        feedType: "home"
    },
    setupController: function(controller, model) {
        this.feedSettings.content =  this.store.findAll('feedstory');
        controller.set('model', model);
        // console.log(model);
    },
    activate: function() {
        App.set("title", "Welcome!");
    }
});

1 个答案:

答案 0 :(得分:3)

现在看起来你的PromiseArrayPromise都是Array。你有没有尝试解决承诺:

this.get('model').then(function(model) {
  model.forEach(function(story) {
    mutableComments.pushObject(story);
  });
});

但是要知道then将执行异步。