ember.js - Ember-Data store.filter with async relationships -
i working on survey application , using existing api. our models like:
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') });
i return live record array model hook via store.filter
filter needs deal both survey's , async participant record current user. how can handle async relation resolution in filter callback function?
model: function() { return ember.rsvp.hash({ user: this.store.find('user', 1), surveys: this.store.filter('survey', {}, function(survey) { return !survey.get('locked'); //how participation record current user current poll can filter out completed true }) }); }
if using live record array of survey's not best way deal is?
edit: i've updated approach try:
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
however, usage of async property participation.get('poll.locked')
in filter pose potential problem?
had written response in es6 , ember-cli format, while localising ember references... please excuse if touch basic reverted es5 , used commonly understood code structure ember.
try this:
// beforemodel() , model() skipped if coming collection // ie: '/users' '/users/1' // setting purely direct linking route's path. model: function(params) { return this.store.findrecord('user', params.id).then(function(user) { return user.get('participations'); }); }, // fired once! // obsolete... setupcontroller: function(controller, model) { controller.set('model', model); var store = this.store, userid, availsurveys, completed, outstanding; store = this.store; userid = model.get('id'); // promise! // also, these filters can applied elsewhere store available! availsurveys = store.filter( // modelname filtered. 'surveys', // part query - sent request server, not used filter { locked: false }, // active filter applied survey records in client, // updating 'availsurveys' 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) { // promise! return participation.get('survey').then(function(survery) { return !participation.get('hascompleted') && !survey.get('locked'); }); }); // alternatively, hashsettled waits until 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); }); }
Comments
Post a Comment