Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4840

Saving hasMany for existing objects

$
0
0

@Chancer wrote:

I'm learning ember by writing a small video catalog app.

The models look like so:

export default DS.Model.extend({

    name:        DS.attr('string'),
    year:        DS.attr('number'),
    categories:    DS.hasMany('category')
});

and

export default DS.Model.extend({    
    name:     DS.attr('string'),
    videos: DS.hasMany('video')
});

I have created forms for capturing new categories and new videos.

For the new videos form, I use a multiselect-checkbox to enable the user to select the categories the title belongs to. These are pulled from the model in the videos/new route:

export default Ember.Route.extend({
    model: function() {
        return this.store.findAll('category');
}});

I'm not really sure how I should be saving the category along with the video - at the moment my videos/new createVideo function looks like this:

createVideo: function() {

    // movieCategories map stores the category.id => category.name

    let newVideo = this.store.createRecord('video', {name: this.get('name'), year: this.get('year') });

    let cats = newVideo.get('categories');

    for (var key in this.get('movieCategories'))
    {

        let rec = this.store.findRecord('category', {id: key.toString()});

        if (rec) { cats.pushObject(rec); }
    }

    newVideo.set('categories', cats);
    newVideo.save();

}

I can't seem to find any documentation or examples on this, any links or explanations greatly appreciated.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4840

Trending Articles