簡體   English   中英

使用forEach()的Ember訪問模型數組

[英]Ember Accessing Model Array with forEach()

我遇到了將ember-data從0.X升級到1.0.0-beta12 我的ember.js版本是1.7.1 在我的控制器中,我有一個方法contentChanged():它遍歷我的模型:

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

自從為this.get('model');升級ember-data結構以來this.get('model'); 已經改變,所以forEach()找不到我的數組。

更新前: 之前

更新后:

后

內容最初是一個array[20]但現在它是一個具有內容屬性array[20] 我需要迭代這些項目,但當前的代碼不起作用。

我努力了。 this.get('model').toArray()但是這返回了一個空數組。 如何訪問內容數組?

編輯:要添加更多上下文,應用程序使用此代碼迭代故事對象並將它們放在currentStories數組中。 我們使用帶有Ember.Mixin 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!");
    }
});

這看起來真的像你現在有一個PromiseArray ,它是PromiseArray 你有沒有試過解決承諾:

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

但是知道then會執行異步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM