javascriptember.jsember-datastore

Avoid showing new model itself


I have got a form to create a new Model called Route. In the form is a select field, called takeover, showing all Routes and the user can select one to get an relationship to the selected Route. My problem is that the newly created Route is even selectable in the select field, because I simply return 'this.get('store').findAll('routes');'. How can I remove the new Model from the selection, because it is not possible to create a relationship to itself.

In the Route I create a empty Model and then the user can modify the attributes within the form:

//route.js
model() {
    return this.store.createRecord('route', {});
  },

Template:

//route.hbs
<form>
//some fields
//and a select-component:
{{my-select
    selected=model.takeover
    options=routes
}}
</form>

the routes which I give to the selectable options also in the route:

//route.js
 routes: Ember.computed(function () {
    return this.get('store').findAll('route');
  }).readOnly(),

So routes are all routes including the new model. But it shouldn't be able to select the model itself so I have to remove it out of routes. How can I do it?


Solution

  • Try this,

    routes: Ember.computed(function () {
        return this.get('store').findAll('route').then(function(result){
         return result.filterBy('isNew', false);
        });
      }).readOnly(),
    

    Update:

    If possible don't have route or routes as your route name and computed property name.

    In route.js

    model(){
     return Ember.RSVP.hash({
      newRecord: this.store.createRecord('route', {}),
      routes : this.get('store').findAll('route').then(function(result){
         return result.filterBy('isNew', false);
        }),
      });
    },
    setupController(controller,model){
     this._super(...arguments);
     controller.set('NewRecordModel',model.newRecord);
     controller.set('routes',model.routes);
    }
    

    and in route.hbs template,

    {{my-select
        selected=NewRecordModel.takeover
        options=routes
    }}