javascript - Ember.js - List values from store -


from route or component, want create array of values store data. e.g. have project model , want list of project names can use in select boxes, table headings...

i've tried following , looked @ computed properties, can't working?

// user route  model() {      let projects = this.store.findall('project').then((projects) => {         return projects.mapby('name');     });      ... 

update:

// users.new route import ember 'ember';  export default ember.route.extend({      projectnames: [],      aftermodel: function () {         this._super(...arguments);         return this.store.findall('project').then((projects) => {             this.set('projectnames', projects.mapby('name'));         });     },      setupcontroller: function (controller) {         this._super(...arguments);         controller.set('projectnames', this.get('projectnames'));     },      model() {         let user = this.store.createrecord('user'),             projectroles = [],             projects = this.get('projectnames');              console.log('projectnames: ' + this.get('projectnames'));          projects.foreach((project) => {             let projectrole = this.store.createrecord('projectrole', {                 project: project,                 role: 'viewer'             });             projectroles.push(projectrole);         });          // create 1 role per project         ember.rsvp.all(projectroles.map(projectrole => projectrole.save())).then((projectroles) => {             user.set('projectroles', projectroles);         });          return user;     } } 

i think basics of need can answered beforemodel() hook. beforemodel() hook returns promise, fulfilled before model() hook called. lets load list of projects first. following code (untested) should there:

// services/project-core.js import ember 'ember';  export default ember.service.extend({   store: ember.inject.service(),   projects: null,    init() {     this.set('projects', []);   },    // getprojects() needs called @ least once before projects available in application. ideally method called in route's beforemodel() hook.   getprojects() {     if (this.get('projects').length === 0 {       // makes sure query projects once; may not want block. avoid unneeded queries.       return this.get('store').findall('project').then(projects => {         this.set('projects', projects);       });     } else {       return ember.rsvp.hash({});     }   } }); 

and route, changes:

// users.new route import ember 'ember';  export default ember.route.extend({   projectcore: ember.inject.service(),    beforemodel() {     return this.get('projectcore').getprojects();   },    model() {      // here you'll able things like:     this.set('projectnames', this.store.peekall('project').map(function(x) {return x.get('name')}));      // , code:     let user = this.store.createrecord('user'),         projectroles = [],         projects = this.get('projectnames');      console.log('projectnames: ' + this.get('projectnames'));      projects.foreach((project) => {       let projectrole = this.store.createrecord('projectrole', {           project: project,           role: 'viewer'       });       projectroles.push(projectrole);     });      // , on...   } } 

if want use projects in template, can use them so:

<ul>   {{#each projectcore.projects |project|}}     <li>{{project.name}}</li>   {{/each}} </ul> 

and if want projectnames array available, remember use setupcontroller did in question.

in fact, in experience want use projectnames array, instead better off iterating through set of projects. particularly true in templates.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -