我应该如何从emberjs中的子控制器重新加载模型?

时间:2016-01-20 12:09:38

标签: ember.js ember-data

我在尝试更新模型值时遇到问题,当调用 PendingActionController.updateStage 方法时,我需要它来更新相关模型&反映更新的值。如果我在PendingController中创建另一个方法,如 ShowMessage ,则会显示警告。

请解释我应该使用什么方法?

例如,以下是代码:

<script type="text/x-handlebars" id="pending/_actions">
<div class="content-actions">
    <h2>Pending Actions</h2>
    <ul>
        {{#each pendingstages}}
            <li>
                {{#unless refreshingStage}}
                    {{render 'pendingAction' this}}
                {{/unless}}
            </li>
        {{/each}}
    </ul>
</div>
</script>
<script type="text/x-handlebars" id="pendingAction">    
    <div class="actionsBox">
        <div class="actionsBar">
            <div {{bindAttr class=":actionStatus completed:blue:green"}} {{action updateStage this}}>&nbsp;</div>
        </div>
        <div class="clear-both"></div>
    </div>
</script>

PendingController:

App.PendingController = App.BaseObjectController.extend(App.ActionsControllerMixin, {
needs: ['application'],
postRender: function () {   
    //Some code here....
},

pendingstages: function(){
    return App.PendingStage.find({Id: this.get('model.id')});
}.property('model.id', 'model.@stages.completed', 'refreshStage'),

ShowMessage: function(){
   alert('Inside Sohw message.');
},
});

PendingActionController

App.PendingActionMixin = {
    isEditing: false,
    canDelete: true,
    canEdit: true,

    toggleIsEditing: function(){
        this.toggleProperty('isEditing');
    }
};

App.PendingActionController = App.BaseObjectController.extend(App.PendingActionMixin, {  
    needs: 'pending',   
    postRender: function(){
        //some code here...
    },

    updateStage: function(stage){
        var self = this;
        this.get('controllers.pending').send('pendingstages');      
    },
});

编辑(1): Followignt是Ember&amp;的版本。烬数据: 烬-1.0.0-master.js ember-data-master.js:CURRENT_API_REVISION:12

1 个答案:

答案 0 :(得分:0)

使用store.fetch代替store.find可以解决问题。

store.fetch始终调用API,无论该特定数据是否存在于本地ember-data存储中。像这样使用..

pendingstages: function(){
    return App.PendingStage.fetch({Id: this.get('model.id')});
}.property('model.id', 'model.@stages.completed', 'refreshStage'),

请参阅ember-data/store.js code。它现在已被弃用。但你会找到新方法而不是这个。