2015-05-12 41 views
1

我正在調查應用程序,我們正在使用現有的API。我們的模型是這樣的:Ember-Data store.filter與異步關係

App.User = DS.Model.extend({ 
    name: DS.attr('string'), 
    participations: DS.hasMany('participation', {async: true}) 
}); 

App.Participation = DS.Model.extend({ 
    user: DS.belongsTo('user', {async: true}), 
    survey: DS.belongsTo('survey', {async: true}), 
    hasCompleted: DS.attr('boolean'), 
    hasAccepted: DS.attr('boolean') 
}); 

App.Survey = DS.Model.extend({ 
    participations: DS.hasMany('participation', {async: true}), 
    title: DS.attr('string'), 
    locked: DS.attr('boolean') 
}); 

我想通過store.filter但是這個過濾器需要同時面對調查的和當前用戶的異步參與者記錄從我的模型鉤回的現場記錄陣列。我如何處理我的過濾器回調函數中的異步關係解析?

model: function() { 
    return Ember.RSVP.hash({ 
     user: this.store.find('user', 1), 
     surveys: this.store.filter('survey', {}, function(survey) { 
     return !survey.get('locked'); //How do I get the participation record for the current user for the current poll so I can also filter out the completed true 
     }) 
    }); 
    } 

如果使用一個現場調查記錄陣列不是處理這個問題的最佳方法是什麼?

編輯: 我已經更新的辦法嘗試:

App.SurveysRoute = Ember.Route.extend({ 
    model: function() { 
    return Ember.RSVP.hash({ 
     user: this.store.find('user', 1), 
     all: this.store.find('survey'), 
     locked: this.store.filter('survey', function(survey) { 
     return survey.get('locked'); 
     }), 
     completed: this.store.filter('participation', {user: 1}, function(participation) { 
     return participation.get('hasCompleted'); 
     }), 
     outstanding: this.store.filter('participation', {user: 1}, function(participation) { 
     return !participation.get('hasCompleted') && !participation.get('poll.locked'); 
     }) 
    }); 
    } 
}); 
App.SurveysCompletedRoute = Ember.Route.extend({ 
    model: function() { 
    return this.modelFor('surveys').completed.mapBy('survey'); 
    } 
}); 

http://jsbin.com/vowuvo/3/edit?html,js,output

然而,不異步屬性participation.get('poll.locked')在我的過濾器的使用帶來一個潛在的問題?

回答

0

最初寫了我的ES6和ember-cli格式的迴應,雖然本地化Ember引用...請原諒這是否是一個基本的觸摸,因爲我將它恢復爲ES5並使用通常理解的Ember代碼結構。

試試這個:

// beforeModel() and model() are skipped if coming from a collection 
// ie: from '/users' to '/users/1' 
// setting this up is purely for direct linking to this route's path. 
model: function(params) { 
    return this.store.findRecord('user', params.id).then(function(user) { 
     return user.get('participations'); 
    }); 
}, 

// only fired once! 
// soon to be obsolete... 
setupController: function(controller, model) { 
    controller.set('model', model); 

    var store = this.store, 
     userId, availSurveys, completed, outstanding; 

    store = this.store; 
    userId = model.get('id'); 

    // this is a promise! 
    // also, these filters can be applied elsewhere that Store is available! 
    availSurveys = store.filter(
     // modelName to be filtered. 
        'surveys', 
     // this part is the query - sent as a request to server, not used as a filter 
        { locked: false }, 
     // this is the active filter that will be applied to all survey records in client, 
     // updating 'availSurveys' as the records change 
        function(survey) { 
         return !survey.get('locked'); 
        }); 

    completed = store.filter('participation', 
        { 
         user   : userId, 
         hasCompleted : true 
        }, 
        function(participation) { 
         return participation.get('hasCompleted'); 
        }); 

    outstanding = store.filter('participation', 
        { 
         user   : userId, 
         hasCompleted : false, 
         survey  : { locked: false } 
        }, 
        function(participation) { 
         // this is also a promise! 
         return participation.get('survey').then(function(survery) { 
          return !participation.get('hasCompleted') && !survey.get('locked'); 
         }); 
        }); 

    // alternatively, hashSettled waits until all promises in hash have resolved before continuing 
    Ember.RSVP.hash({ 
     availSurveys : availSurveys, 
     completed : completed, 
     outstanding : outstanding 
    }).then(function(hash) { 
     controller.set('availSurveys', hash.availSurveys); 
     controller.set('completed', hash.completed); 
     controller.set('outstanding', hash.outstanding); 
    }); 
}