ember.js - Ember filter store by current user -


i'm little bit lost , struggling find correct information guide me. have application selling coffee, , when user logs in, i'd filter data see store they're apart of.

(i'm going capitalize store when talking coffee store object, confusion in relation ember data store)

export default ds.model.extend({   user: ds.belongsto('user'),   active: ds.attr('boolean'),   name: ds.attr('string'),   extrashotprice: ds.attr('number'),   syrupprice: ds.attr('number'), }); 

here store model, , it's linked user in simple belongsto relationship.

on backend, have endpoint returns current user object properly, i'm little lost @ how/where can load user store , use in controller.

the second thing need filter model in route user.

export default ember.route.extend({   model: function(){     // not sure user.id come from?     return this.store.find('store', {'user': user.id})   } }); 

right i'm hardcoding model because i've been stuck on while, , works expected hardcoded value.

by way, store , user have 1-to-1 relationship. suggestions!

a service can choice long-lived object in ember (such current_user in many applications. it's surprisingly simple , don't know why it's not discussed more:

services/session_service.js

export default ember.service.extend({   currentuser: ember.computed(function() {     // fetch user     // use dummy data     return ember.object.create({       name: "kori",       worktitle: "mr. big boss",       skills: ["ember", "mentoring", "ruby", "refactoring"]     });   }) }); 

controllers/some_controller.js

export default ember.controller.extend({   session: ember.inject.service(),   currentuser: ember.computed.alias("session.currentuser") }); 

usage in template:

<h2>user details:</h2> <p>title: ({{currentuser.worktitle}})</p> <p>name: {{currentuser.name}}</p> <p>mad skills:</p> <ul>   {{#each currentuser.skills |skill|}}     <li>       {{skill}}     </li>   {{/each}} </ul> 

in specific case might end route looking this:

export default ember.route.extend({   session: ember.inject.service(),   model: function(){     return this.store.find('store', {'user': this.get('session.currentuser.id')})   } }); 

then ocd kicks in:

export default ember.route.extend({   session: ember.inject.service(),   currentuserid: ember.computed.alias('session.currentuser.id'),   model: function(){     return this.store.find('store', { 'user': this.get('currentuserid') })   } }); 

p.s. store model name == no end of fun!

sample codepen


Comments