0

I am trying to set data from two models (that has hasMany & belongsTo relationship) and save them to firebase.

'list' data ends up being saved to firebase but not user data.

I think I'm doing something wrong at step 3. I'd appreciate your help!

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.find('list');
    },
actions: {
    createList: function() {
        var newListTitle = this.controllerFor('lists').get('newListTitle');
        var username = this.get('session.user.displayName');
        alert(this.get('session.user.displayName'));

        if (Ember.isBlank(newListTitle)) { return false; }

    //1
        var list = this.store.createRecord('list', {
            title: newListTitle,
            user: username,
        });

    //2
        this.controllerFor('lists').set('newListTitle', '');

        var _this = this;

    //3
            list.save().then(function(list) {
                user.get('lists').addObject(list);
                user.save();
                _this.transitionTo('lists.show', list); //4
            });

        }
    }
});
sunoceansand
  • 237
  • 3
  • 13

1 Answers1

0

Restructured your adding logic as well as user defined models, also modified your route, which could look like this in Edit and View mode. Meaning you can have more than one item returned from "model".

// Update models

App.List = DS.Model.extend({
     value:     DS.attr('string')
});

App.User = DS.Model.extend({
    name:   DS.attr('string')
});

App.UserLists = DS.Model.extend({
   user: DS.belongsTo('user'),
   list: DS.belongsTo('list')
});



    export default Ember.Route.extend({
        LIST:SHOW ROUTE
        model: function(params) {

            var store = this.get('store');
            var userPromise = store.find('user', params.id);

            return Ember.RSVP.hash({
                user: userPromise,
                userList : userPromise.then(function(user) {
                    return store.find(userList, { WhereUserIdIs : user.get('id') })
                });
            });
        },
    actions: {
        createList: function() {
            var self = this;

            var failure = function(reason) {
                // handle stuff
            };

            var list = this.store.createRecord('list', {
                title: this.get('title'),
            });

            var user = this.get('user');

            var usersList = store.createRecord('userList', {
                'user': user,
                'list': list
            });

            list.save().then(function(list) {
                user.save().then(function() {
                    userList.save().then(function() {
                        self.transitionTo('lists.show', list.get('id'));
                    }, failure);
                }, failure);
            }, failure);

        }
    });
kristjan reinhold
  • 2,038
  • 1
  • 17
  • 34
  • Thank you! I appreciate that. It's saying that userList is undefined, is there a way I can define it somewhere? – sunoceansand Apr 28 '15 at 15:07
  • Also why is there no mention of this.store.find('list');? – sunoceansand Apr 28 '15 at 15:13
  • @sunoceansand well you should define your models this way, list,user and usersList =) also make sure your backend has the same behaviour – kristjan reinhold Apr 28 '15 at 15:34
  • @sunoceansand Reason there's no mention of this.store.find('list') - I query for userList with Given user as queryparameter.. this should end up like this. ... http.../userslist/?user=1 and it woud return object with only users that have id of 1 . updated post for models – kristjan reinhold Apr 28 '15 at 15:36
  • hmm why would that be better than ... user: DS.belongsTo('user', {async: true})... list: DS.hasMany('list', {async: true}), ? – sunoceansand Apr 28 '15 at 15:51
  • @sunoceansand You can use hasMany to display business logics in hbs template, but that does not apply to saving. Atleast not in my case, maybe some adapters are built to make things happen this way. – kristjan reinhold Apr 28 '15 at 18:42