PCR
PCR

Reputation: 275

ember - combining data for two models into single result for power sort

I'm getting data from two models in one of my routes using RSVP hash, and then trying to combine those results in my controller so that they can be used in a select for power sort. However something doesn't seem to be working.

My route looks like this:

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        return Ember.RSVP.hash({
            newBook : this.store.createRecord('book'),
            authors : this.store.findAll('author'),
            publishing_houses : this.store.findAll('publishing-house')    
        });
    },

    setupController(controller, model) {
        this._super(...arguments);
        Ember.set(controller, 'authors', model.authors);
        Ember.set(controller, 'publishing_houses', model.publishing_houses);
    },

    actions: {
        save() {
            this.modelFor(this.routeName).get('newBook').save(); 
        }
    }    
});

My template looks like this:

<form {{action "save" on="submit"}}>
    {{input value=model.newBook.title placeholder="Title"}}<br>
    {{input value=model.newBook.price placeholder="Price"}}<br>

    {{#power-select class="select"
        selected=model.newBook.author
        options=model.authors
        onchange=(action (mut model.newBook.author)) as |author|}}
        {{author.name}}
    {{/power-select}}

    {{#power-select class="select"
        selected=model.newBook.publisher
        options=model.publishers
        onchange=(action (mut model.newBook.publisher)) as |publisher|}}
        {{publisher.name}}
    {{/power-select}}

    <input type="submit" value="Save">
</form>

and my controller, which I think is the problem looks like this:

import Ember from 'ember';

export default Ember.Controller.extend({
    publishers: function() {
        var authors = this.get("authors");
        var publishingHouses = this.get("publishing_houses");
        return authors.concat(publishingHouses);
    }
});

I'm still figuring out how to use controllers. Am I accessing the model data correctly in the controller? Also is this the proper way to create a property to be used in a template?

Upvotes: 0

Views: 415

Answers (1)

Ember Freak
Ember Freak

Reputation: 12872

In setupController hook, you don't need to explicitly set authors and publishing_houses since it will be set by default through super function call.

In your controller, you can try accessing it like this.get("model.authors") and in the same way for other properties publishing_houses, newBook

To access RSVP return model from route, you should access it directly without get function.

`this.modelFor(this.routeName).newBook.save()

For concatenation and other stuff you refer http://emberjs.com/api/classes/Ember.Enumerable.html

reduce might suite your needs.

Upvotes: 0

Related Questions